Executable size

Started by
10 comments, last by level10boy 21 years, 7 months ago
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
Advertisement
Yes. But note that there will only ever be one copy of the member function anyway, regardless of how many objects of that class you instantiate.
virtual function addresses are put into the vtbl regardless of whether they are called I thought... but again you only have one vtbl per class.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
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
I thought the vtbl was a static const ?
daerid@gmail.com
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
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]
It's not what you're taught, it's what you learn.
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
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.
william bubel
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]
It's not what you're taught, it's what you learn.

This topic is closed to new replies.

Advertisement