怎么看C++对象的内存结构 和 怎么解密C++的name mangling

Dumping a C++ object’s memory layout with Clang这篇文章讲述了怎么用Clang来看C++对象的结构,回复中Marek提到了怎么在Visual Studio中看。具体方法如下:

C++项目右键属性,C/C++下的Command Line,加上这个选项

/d1reportAllClassLayout

这样在编译时就会在output窗口看到所有的对象的内存结构了。

由于C++编译器会做Name Mangling,我们可以用undname这个工具来看到没有被mangling样子。

>undname
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.

Usage: undname [flags] fname [fname...]
   or: undname [flags] file

 

下面是个例子,假设我们有如下的A和B两个类。

class A
{
    int a;
public:
    void virtual foo(){};
};

class B:public A
{
    int b;
public:
    void foo(){};
};

它们通过/d1reportAllClassLayout的结果如下:

1>  class A    size(8):
1>      +---
1>   0    | {vfptr}
1>   4    | a
1>      +---
1>  
1>  A::$vftable@:
1>      | &A_meta
1>      |  0
1>   0    | &A::foo
1>  
1>  A::foo this adjustor: 0
1>  
1>  
1>  class B    size(12):
1>      +---
1>      | +--- (base class A)
1>   0    | | {vfptr}
1>   4    | | a
1>      | +---
1>   8    | b
1>      +---
1>  
1>  B::$vftable@:
1>      | &B_meta
1>      |  0
1>   0    | &B::foo
1>  
1>  B::foo this adjustor: 0

posted on 2012-12-23 23:01  fresky  阅读(1577)  评论(0编辑  收藏  举报

导航