How to define class sizeof with virtual functions?

This is a homework question. For the following code

#include <iostream> using namespace std; class A { public: virtual void f(){} }; class B { public: virtual void f2(){} }; class C: public A, public B { public: virtual void f3(){} }; class D: public C { public: virtual void f4(){} }; int main() { cout<<sizeof(D)<<endl; } 

Conclusion: 8

Can someone explain how this is 8 bytes? If the implementation of vtable depends on the compiler, what should I answer this question in an interview? What about virtual base classes?

EDIT: I work on a 32-bit platform.

+6
c ++ sizeof vtable
source share
4 answers

This, of course, is implementation dependent. And that would make a terrible interview question. A good C ++ programmer can simply trust sizeof to be right and let the compiler worry about those vtable things.

But what happens here is that a typical vtable-based implementation requires two vtables in objects of class C or D Each base class needs its own vtable. New virtual methods added by C and D can be handled by extending the vtable format from the same base class, but the vtables used by A and B cannot be combined.

The pseudo-C code here describes how the most derived type D object scans my implementation (g ++ 4.4.5 Linux x86):

 void* D_vtable_part1[] = { (void*) 0, &D_typeinfo, &A::f1, &C::f3, &D::f4 }; void* D_vtable_part2[] = { (void*) -4, &D_typeinfo, &B::f2 }; struct D { void** vtable_A; void** vtable_B; }; D d = { D_vtable_part1 + 1, D_vtable_part2 + 1 }; 
+13
source share

In this question, if you try to get Sizeof class A, it will give you the answer "4", because A has only one virtual function, so its __vptr will contain 4 bytes.

In the same way, if you try to get Sizeof of class B, it will give you the answer โ€œ4โ€, because B also has only one virtual function, so its __vptr will contain 4 bytes.

But class C inherits both classes A, while B and C have a virtual function. This way, C will get 2 __vptr pointers, and C will use the inherited __vptr for its own virtual function. Therefore, if you try to get Sizeof class C, it will give you the answer "8", because C has two virtual pointers.

And finally, class D inherits class C, so D will use the inherited __vptr for its own virtual function and because class C has sizeof '8' bytes, so sizeof D will give you an answer of "8" bytes.

+1
source share

Forgive me for the nebula, but you say that this is homework in nature.

See what sizeof () returns for other classes. Your answer will depend on your compiler and on whether you are in a 32 or 64-bit environment.

Happy shadowing!

0
source share

The size of an object is not related to how many methods it has , and whether these methods are virtual or not. The size of an object is determined solely by its member variables.

I canโ€™t say exactly why you are getting 8 bytes in size. Since there is no data in your class, the C ++ compiler can basically generate a class that takes up no space at all [1]! I assume that 8 bytes is the minimum required to point to a pointer to vtbl, plus possibly overlay.

[1] I think. There is no time to check the specification to see if sizeof can ever return 0.

-2
source share

All Articles