Jump to content
  • Advertisement
Sign in to follow this  
Basiror

C++: sizeof(class with virtual functions)

This topic is 4605 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

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?

Share this post


Link to post
Share on other sites
Advertisement
For 32bit x86 systems sizeof(base) == 4 and sizeof(derived) == 8 on almost any compiler.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!