Is there a tool that I can use to see the multiple inheritance memory layout of compiled C++ code?
A:
Yes, but you're not going to like it much. What you need to do is use the -S flag (on gcc, check your documentation for other compilers) and generate the assembler code, then read that.
Charlie Martin
2008-12-27 04:24:36
+6
A:
I don't know what exactly you want to know. For this simple example
class classA { };
class classB { };
class classC : public classA, public classB {
};
$ g++ -fdump-class-hierarchy test.cpp
Outputs the following into a file test.cpp.002t.class
Class classA
size=1 align=1
base size=0 base align=1
classA (0xb7b06780) 0 empty
Class classB
size=1 align=1
base size=0 base align=1
classB (0xb7b067bc) 0 empty
Class classC
size=1 align=1
base size=1 base align=1
classC (0xb7a736e0) 0 empty
classA (0xb7b067f8) 0 empty
classB (0xb7b06834) 0 empty
See the gcc manpage for details. Changing classA to this:
class classA { int aObj; virtual void aFun() { } };
Suddenly pops up a virtual table:
Vtable for classA
classA::_ZTV6classA: 3u entries
0 (int (*)(...))0
4 (int (*)(...))(& _ZTI6classA)
8 classA::aFun
Class classA
size=8 align=4
base size=8 base align=4
classA (0xb7b4d7f8) 0
vptr=((& classA::_ZTV6classA) + 8u)
Class classB
size=1 align=1
base size=0 base align=1
classB (0xb7b4d9d8) 0 empty
Vtable for classC
classC::_ZTV6classC: 3u entries
0 (int (*)(...))0
4 (int (*)(...))(& _ZTI6classC)
8 classA::aFun
Class classC
size=8 align=4
base size=8 base align=4
classC (0xb7aba820) 0
vptr=((& classC::_ZTV6classC) + 8u)
classA (0xb7b4da14) 0
primary-for classC (0xb7aba820)
classB (0xb7b4da50) 0 empty
What looks so strange (_ZTI6classA and _ZTI6classC and _ZTV6classC) is actually a pointer to the RTTI information gcc created for the classes. Using -fno-rtti
shows that it will assign a null pointer to the second entries of the vtables then. Nice, have fun digging in those information.
Johannes Schaub - litb
2008-12-27 04:33:51
Okay, that's cool.
Charlie Martin
2008-12-27 13:53:22