/d1reportAllClassLayout – Dumping Object Memory Layout

Roman just posted a nice investigation he did, mostly using the g++ switch -fdump-class-hierarchy – which dumps to stdout a graphical representation of generated classes layout.

VC++ has no official similar switch, but deep inside  its undocumented pile of goodies lies the /d1reportAllClassLayout compiler switch, which achieves identical results.   It has made its first (AFAIK) public appearance here, and is mentioned in an official MS support reply and sporadically in other places. An individual object layout dump is available too, as -/d1reportSingleClassLayout.

To see it in action, compile the following with /d1reportAllClassLayout:

class A
{
  int m_a, m_b;
  virtual void cookie() {}
  virtual void monster() {}
}

class B : public A
{
  double m_c;
  virtual void cookie() {};
}

And observe the output (excerpt):

class A size(12):
+—
0 | {vfptr}
4 | m_a
8 | m_b
+—
A::$vftable@:
| &A_meta
|  0
0 | &A::cookie
1 | &A::monster
A::cookie this adjustor: 0
A::monster this adjustor: 0
class B size(24):
+—
| +— (base class A)
0 | | {vfptr}
4 | | m_a
8 | | m_b
| +—
| <alignment member> (size=4)
16 | m_c
+—
B::$vftable@:
| &B_meta
|  0
0 | &B::cookie
1 | &A::monster
B::cookie this adjustor: 0

As the VC team blog post demonstrated, this can be an irreplaceable tool in tracking down obj-compatibility problems – which can go uncaught by the compiler. It is also a great learning tool, and since /d1reportAllClassLayout dumps all compiled objects, including CRT internals – this it is also a unique reversing tool, already used for some deep spelunking into VC++ internals.

This entry was posted in Debugging, VC++. Bookmark the permalink.

3 Responses to /d1reportAllClassLayout – Dumping Object Memory Layout

  1. Alan Ning says:

    Excellent post. I love to see those alignment information in the report.

    Thanks!

    … Alan

  2. Pingback: VC++ 2013 class Layout Change and Wasted Space | Random ASCII

Leave a comment