0
  GCC/32Bit architecture  

class B
    {
        int x,y;
    };
    class D1: public virtual B
    {
        int z;
    };
    class D2; public virtual B
    {
        int z;
    public:
    virtual void func(){}
    };

    int main() {
        B b; D1 d1; D2 d2;
        cout<<sizeof(b)<<endl;
        cout<<sizeof(d1)<<endl;
        cout<<sizeof(d2)<<endl;
        return 0;
    }  

As per my understanding, B has 2 integer: 8 bytes D1: offet to B(B _vPtr), x,y,z => 16. D2: B _vBase, VPTR, x, y => 16

       Ans i am getting is 8, 24, 24.
       What is the size of the class and how memory allocated for this classes. 
    How is Vptr and Vtable managed in these cases.
Jasprit
  • 1
  • 1

1 Answers1

0

What compiler are you using? Visual Studio? gcc? clang? What target architecture (32 or 64bits mainly)? You can also have a look at Size of the classes in case of virtual inheritance.

I'm not looking at the layout, but I would say that there is padding between the pointer to the parent and its actual data to have 8byte alignment of the data. This would mean 4bytes + padding(4) + 8bytes of parent data + local data + padding?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • Is there any other link to understand more in detail regarding above queries. I have used 32 bit and gcc – Jasprit Oct 06 '18 at 09:38
  • https://shaharmike.com/cpp/vtable-part3/ also has a complete exploration of the members of these types of classes (although it doesn't show the layout for just the virtual classes but for the full diamond, as it's the only usage of virtual inheritance that I'm aware of). – Matthieu Brucher Oct 06 '18 at 09:42