Archived

This topic is now archived and is closed to further replies.

Executable size

This topic is 5587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Lets say we have a class that contains 10 member functions. Now if we instantiated an object of this class and we only used 2 out of the 10 member functions, can I assume that any half decent compiler will only generate code for those 2 member functions and not all 10. Think of it this way, if we instantiated 1000 objects of the same class, an still only used the same 2 member functions on all objects, will the compiler still only generate code for the 2 that were used, (assuming the class is not templated). Thanks for any replies

Share this post


Link to post
Share on other sites
Isn''t there one v-table per object? I don''t know much about v-tables though... I heard that it is a constant cost; if you add more virtual functions, it won''t grow. Maybe is there one address / inherited class to support polymorphism.

Cédric

Share this post


Link to post
Share on other sites
I made a little search.

From http://developer.apple.com/techpubs/macosx/Essentials/Performance/Languages/C_Performance_Notes.html
"V-table" is short for virtual function table. A v-table is an array of pointers to virtual methods and support code. Every C++ object containing one or more virtual methods contains a v-table.

And from the C++ FAQ Lite
The compiler creates a v-table for each class that has at least one virtual function. For example, if class Circle has virtual functions for draw() and move() and resize(), there would be exactly one v-table associated with class Circle, even if there were a gazillion Circle objects, and the v-pointer of each of those Circle objects would point to the Circle v-table. The v-table itself has pointers to each of the virtual functions in the class. For example, the Circle v-table would have three pointers: a pointer to Circle::draw(), a pointer to Circle::move(), and a pointer to Circle::resize().



I would trust the C++ FAQ Lite, but OTOH, if every object had a complete v-table instead of a pointer to one, there would be one less indirection. Maybe is it compiler-dependant.

Cédric

Share this post


Link to post
Share on other sites
I think true polymorphism requires that each object have its own v-table (setting one object equal to another copies the v-table over as well)

This way you can set a base object equal to a derived object, and the base object will then use the derived class's virtual function set via the v-table.

EDIT: Not true, according to my experimentation. See my post below.


[edited by - Waverider on August 28, 2002 5:19:22 PM]

Share this post


Link to post
Share on other sites
I''m not sure what you mean there. If we assign pointers, clearly, the v-table won''t be copied. If we assign objects:

MyBase = MyDerived

There will be slicing. So if the v-table is copied, it will be highly dangerous, because the functions of MyDerived will be called on a MyBase!

Cédric

Share this post


Link to post
Share on other sites
Object orientated programming is up there with the idea of reentrant code and multitasking/multithreading, etc. Anyways, since the member function''s code never changes, the code is only stored once. The only thing that realy changes is the implied pointer ("this").

Though, I think that most object orientated languages stores an instanciated object with pointers to functions... I''m not terribly sure, i haven''t investigated this, but its the first thing that comes to mind on how to implement virtual functions and stuff.

-> Will Bubel
-> Machine wash cold, tumble dry.

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
I'm not sure what you mean there. If we assign pointers, clearly, the v-table won't be copied. If we assign objects:

MyBase = MyDerived

There will be slicing. So if the v-table is copied, it will be highly dangerous, because the functions of MyDerived will be called on a MyBase!

Cédric


I could be mistaken. But I think that's how this code works:

class A {
virtual void out(void) {printf("Hi!");}
};

class B : public A {
virtual void out(void) {printf("Hoo ya!");}
};

A a;
B b;

a=b;
a.out(); // produces "Hoo ya!" (or so I thought, see below)

This can only happen if v-tables are copied.

Wouldn't the compiler error out if there would be slicing ("Cannot convert from class B to class A")?

EDIT: Hmm - Just tried it in a program and it output "Hi!". There was some other thread that talked about this and it behaved differently. Blah. Forget it.

EDIT 2: Here is the thread in question

EDIT 3: I tested his program just now. It didn't work the way was suggested in the thread. He wrote "It works now, thanks" and I'm like HUH? I should have tested it then.


[edited by - Waverider on August 28, 2002 5:29:55 PM]

Share this post


Link to post
Share on other sites
Bjarne says in TCPPPL that polymorphic behavior can ONLY be exhibited with pointer or reference types.

See below:


  
#include <iostream>

using namespace std;

class A
{
public:
virtual void out() { cout << "Out from class A" << endl; }
};

class B : public A
{
public:
void out() { cout << "Out from Class B" << endl; }
};

void main()
{
A a;
B b;

a.out(); // echoes "Out from class A"

b.out(); // echoes "Out from class B"


A new_a = b;
new_a.out(); // echoes "Out from class A", as expected,

// since we are _COPYING_ the parts of b

// that are of class A over to a new object of class A

A* p_a = &b;
p_a->out(); // echoes "Out from class B", because it''s a pointer

// type, and accesses the vtbl

A& r_a = b;
r_a.out(); // echoes "Out from class B", because (again) it''s a reference type (points-to)

}

Share this post


Link to post
Share on other sites