Size of the Classes in Case of Virtual Inheritance

Size of the classes in case of virtual inheritance

sizeof(A): 8

3 bytes in the array, 1 byte padding, 4 bytes for the vptr (pointer to the vtable)

sizeof(B): 12

A subobject: 8, 3 bytes for the extra array, 1 byte padding

sizeof(C): 16

This is probably the surprising one for you...
A subobject: 8, 3 bytes for the extra array, 1 byte padding, 4 bytes pointer to A

Whenever you have virtual inheritance, the location of the virtual base subobject with respect to the start of the complete type is unknown, so an extra pointer is added to the original object to track where the virtual base is. Consider this example:

struct A {};
struct B : virtual A {};
struct C : virtual A {};
struct D : B, C {};

The location of A with respect to the start of the B object when the complete type is a B can be different than the location of the A subobject of B when it is part of the final object D. If this is not obvious, assume that the relative location is the same, and check whether the relative location of A with respect to C in a C final object or a C subobject in D can also be maintained.

As for the last example, I don't quite feel like analyzing it... but you can read the Itanium C++ ABI for a concrete implementation of the C++ object model. All other implementations don't differ much.


Last example:

sizeof(D): 32

D contains a B subobject (12), and a C subobject (16), plus an additional array of size 3 and one extra bit of padding 1.

In this particular case, the question that could come up is why are there two A subobjects if C inherits virtually from A, and the answer is that virtual base means that the object is willing to share this base with any other type in the hierarchy that is also willing to share it. But in this case B is not willing to share it's A subobject, so C needs it's own.

You should be able to track this by adding logs to the constructors at the different levels. In the case of A have it take a value in the compiler and pass different values from each extending class.

size of derived class in virtual inheritance

Virtual inheritance means, that the virtual base classes only exist once instead of multiple times. That is why the 8 bytes from ClassA are only in ClassD once. Virtual inheritance itself requires a certain overhead and hence you get an additional pointer. The exact implementation and therefore the exact overhead is not specified by the C++ standard and may vary depending on the hierarchy you are creating.

Why size of classes is larger in case of virtual inheritance?

Because when using virtual inheritence, the compiler will create* a vtable to point to the correct offsets for the various classes, and a pointer to that vtable is stored along with the class.


  • "Will create" -- vtables are not dictated by the Standard, but the behaviors implied by virtual inheritence is. Most compilers use vtables to implement the functionality dictated by the Standard.

Virtual inheritance in C++, size of the grand child's object is heavy?

The most common way to implement virtual function and virtual inheritance is through virtual tables (or vtables). These are added as invisible member variables of the classes, adding to the size.

The vtables are usually stored separately which means the invisible member will be a pointer to the table, and on a 64-bit system the pointer size is typically 8 bytes (64 bits), and since you have two virtual classes you have two pointers leading to 16 bytes of extra data.


As for the two bytes in the second case, it is probably because an object can't really be empty. To be able to get an objects size, and more importantly to be able to place object in memory, they need to take up some space, typically a single byte will be enough. If you create an instance of base and gets that instances size it will probably be one.

Why just two bytes instead of just one? You have to check what the compiler does, but the double-inheritance might have something to do with it.

Since the virtual classes already have a size through their invisible vtable pointers, they don't need the extra "empty class" padding.

Size of an array inherited virtually from two classes

It is probably size of v-table for keeping virtual function addresses. If you check b1 or b2, it is 48 bytes, probably a pointer to v-table(pointer on 64 bit system is 8 bytes). same thing happens for next inheritance increasing size by 8 more.

Side note: I think it is v-table because if you compile your code as a 32-bit program, the increase of sizes will be 4 bytes(pointer in 32 bit system). In addition, if you remove virtual keyword the size will not increase anymore, so it is obviously something related to virtual calls and first thing that comes to mind is v-table.

Multiple Inheritance : size of class for virtual pointers?

It depends on compiler implementation. My compiler is Visual Stdio C++ 2005.

Code like this:

int main(){
cout<<"sizeof(B):"<<sizeof(B) << endl;
cout<<"sizeof(C):"<<sizeof(C) << endl;
cout<<"sizeof(D):"<<sizeof(D) << endl;
return 0;
}

It will output

sizeof(B):4
sizeof(C):4
sizeof(D):8

class B has only one virtual pointer. So sizeof(B)=4. And class C is also.

But D multiple inheritance the class B and class C. The compile don't merge the two virtual table.So class D has two virtual pointer point to each virtual table.

If D only inheritance one class and not virtual inheritance. It will merge they virtual table.



Related Topics



Leave a reply



Submit