Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

virtual functions doubt !

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

can some one tell me why size of class d1 is 12 bytes .. should not be it : sizeof(y1) + sizeof(VIRTUAL POINTER for class d1) + sizeof (object of b1) = 4 + 4 + 8 = 16 ..
but the program output shows it to be 12 .. ??

size of b1 : the output shows to be 8 , which is ok : size of int + size of VIRTUAL pointer for class b1 = 4 + 4 = 8

#include<iostream>
using namespace std;

class b1
{
int x;
public:
virtual ~b1(){}
};

class d1: public b1
{
int y1;
public:
virtual ~d1(){}

};

int main()
{
cout<<"sizeof int :" <<sizeof(int);
cout<<"\n size of b1:"<<sizeof(b1);

cout<<"\n size of d1:"<<sizeof(d1);



return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
I don't believe d1 needs b1's virtual pointer.

So b1 has:

int x;
*_vptr_to_b1;
//total = 8 bytes

and d1 has:

int x;
int y1;
*_vptr_to_d1;
//total = 12 bytes;

http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/

Share this post


Link to post
Share on other sites
You only need ONE pointer for each virtual function flow. I mean... If you override a function 4 times in a 4-level hierarchy, you only need one pointer on your class instance that points to your actual implementation.

I'd think it that way:

d1 only needs to store two integers, the one on the base class and its own. The third byte belongs to the pointer to its destructor. No more space is needed.

Share this post


Link to post
Share on other sites

You only need ONE pointer for each virtual function flow.


Actually, you only need 1virtual function table pointer regardless of the number of virtual functions or class structure. So any class with virtual functions should have a sizeof == sizeof(vftable pointer) + sizeof(member data) + platform_alignment_padding.

For any given class, the table of virtual functions is known at compile time. Each object simply has a pointer to the table for its instantiated class. This is what causes a "double jump" for any virtual function table call, once to the vftable and once again to the code.

EDIT:
for more fun trivia: Every platform I've tested on will assign the value of the vftable pointer in the constructor and revert it in the deconstruction phase. Meaning if you happen to hit a debug breakpoint in b1::b1() for a new d1(); then you will see the vftable points to b1's. Once it enters d1::d1() it will point to d1's vftable. Likewise it will roll back during the delete call chain.

I've used this to determine if a pointer I have to a d1 has been destroyed or not (whether its vftable points to b1 or d1)

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!