Search code examples
c++static-analysisdisassemblyobjdump

Getting sizes of class member variables (without running code)


Is there a way to get the sizes of all member variables of a particular class without actually running the code? (i.e no sizeof(), offset_of() operations)

Do objdump or otool have some options to extract this information from the intermediate object files (or even the final ELF file)?

EDIT:

"What am I trying to do?":

I noticed between two builds of our software that a particular class instance ballooned in size. This class is extremely big with many, many member variables. The class in question did not change between the two builds, but its member variables did. I'm trying to find the culprit (and it'll be somewhat of a depth-first search, since I'll have to continue to dig deeper and deeper into each member variable till I find it) and need a scalable method to do this without resorting to printfs() and diffing.


Solution

  • You can create a tool based on Clang to compile your code and dump the record layout of your class while compiling. Then you'll be able to compare them.

    (Maybe there is a similar method for other compilers as well.)