C++ : How can I know the size of Base class SubObject?


Here I was discussing Empty Base Optimization, and MSalters made this interesting comment:

No class can ever have sizeof(Class)==0, empty or not. But we're talking specifically over the size of an empty base class subobject. It doesn't need its own vtable, nor a vtable pointer. Assume the common layout of a vtable pointer at offset 0; that would cause the zero-sized base class subobject to share its vtable pointer with the derived class. No problem: those should be identical anyway, that's pretty much the point of virtual functions.

My question specifically is this: compiler may optimize when we use Empty Class as base class, or it may not. How would we determine as to what it actually does?

And in general, how can we know the size of base class subobject? Is the size of Base subobject same irrespective of whether we use it as base or not? Do compilers optimize only with Empty Base Classes?



Good answers.

By the way, MS VC++ and G++ compilers can dump class layouts for you to study.

With VC++ just run cl.exe /c /d1reportAllClassLayout <source>.cpp

This uses class and vtable layout dump code I wrote in 1990 to test proper layout objects, vtables, vbtables, etc.

To better understand how C++ compilers lay out objects in memory, you might enjoy http://www.openrce.org/articles/files/jangrayhood.pdf and Stan Lippman's book Inside the C++ Object Model.

Happy hacking!

Need Your Help

Is it possible to send WM_QUERYENDSESSION messages to a window in a different process?

winapi message

I want to debug a windows C++ application I've written to see why it isn't responding to WM_QUERYENDSESSION how I expect it to. Clearly it's a little tricky to do this by just shutting the system d...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.