C++: sizeof(class with virtual functions)

Started by
12 comments, last by Basiror 18 years, 3 months ago
Hi if I have a class derived from a abstract base class in C++ like this

class base
{
public:
virtual void somefunction();
}

class derived : public base
{
public:
void somefunction();
unsigned int variable;
}

Does sizeof(derived) return 4 bytes for unsigned int variable or 4 bytes for uint.. plus the size of the vtable pointer?
http://www.8ung.at/basiror/theironcross.html
Advertisement
For 32bit x86 systems sizeof(base) == 4 and sizeof(derived) == 8 on almost any compiler.
so the table pointer is included

thx alot
http://www.8ung.at/basiror/theironcross.html
You can't rely on the size of a structure being the same between architectures, compilers, or even on the same compiler and architecture with different compile options.

That's why sizeof() exists, because the size of a structure is decided by the compiler at compile time.

Mark
Quote:Original post by Basiror
so the table pointer is included

thx alot


Yes, it's included. The C++ compiler basically introduces a hidden member for your class that contains the pointer to the vtable (depends much on how the compiler implements virtual functions).
Now that you know the answer, I wonder why you want to know? There isn't much useful information that can be gotten out of the knowledge.

CM
The nitpicky would tell you that how exactly virtual function lookup works is implementation-dependent and might not be based on vptrs at all.

Even for compilers that do use them once you throw multiple inheritance in the mix you can easily end up with multiple vptrs.

i.e. any use you make of the information coming out of this thread is compiler-dependent and could change even between versions of the same compiler.
-Mike
You may be interested to read about empty base class optimisation as it applies to your code example.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by Conner McCloud
Now that you know the answer, I wonder why you want to know? There isn't much useful information that can be gotten out of the knowledge.

CM


Actually, understanding that the compiler tacks on the hidden member is very useful, especially if you can find out how much is tacked on. This is important if you want to, for example, dump the entire class out to file as raw data (not serialized) and then hope to reconstruct the vftable when you reload it. Which is not something I recommend doing, btw ;)

Heck, anytime you're very concerned about space information like this is very useful =) In certain cases, knowing where the vftable member is placed in the object could also be extremely important :)

Just my 2c :)
Quote:Original post by Grafalgar
Actually, understanding that the compiler tacks on the hidden member is very useful, especially if you can find out how much is tacked on. This is important if you want to, for example, dump the entire class out to file as raw data (not serialized) and then hope to reconstruct the vftable when you reload it.

When would you want to do this?
Quote:Original post by Grafalgar
Which is not something I recommend doing, btw ;)

Then how important can it be?
Quote:Original post by Grafalgar
Heck, anytime you're very concerned about space information like this is very useful =) In certain cases, knowing where the vftable member is placed in the object could also be extremely important :)

Why would you want to do this?

When answering these questions, keep in mind the original poster is clearly a begginer who isn't ready for the sort of advanced topics such knowledge is useful for. If he were, he would have known how to answer that question himself.

CM

This topic is closed to new replies.

Advertisement