使用GDB 分析C++ 类的内存分布


首先,代码如下:

  1 
  2 #include <string.h>
  3 #include <iostream>
  4 
  5 class CFox
  6 {
  7 public:
  8     CFox();
  9     void Print(void);
 10 private:
 11     int a;
 12 };
 13 
 14 CFox::CFox(void)
 15 {
 16     a = 10;
 17 }
 18 
 19 void CFox::Print(void)
 20 {
 21     std::cout<< "this is print for cfox class" << std::endl;
 22 }
 23 
 24 class IMyClass
 25 {
 26 public:
 27     IMyClass(void);
 28     virtual void DoAction(int id) = 0;
 29     virtual void Test(void) = 0;
 30     void Foo(void){}
 31 protected:
 32     int id;
 33     char name[16];
 34 };
 35 
 36 IMyClass::IMyClass(void)
 37 {
 38     memset(name, 0, sizeof(name));
 39     id = 0;
 40 }
 41 class CMyClass : public IMyClass
 42 {
 43 public:
 44     CMyClass(void);
 45     void DoAction(int id);
 46     void Test(void){}
 47 private:
 48     CFox *pFox;
 49 };
 50 
 51 CMyClass::CMyClass(void)
 52 {
 53     pFox = new CFox();
 54 }
 55 void CMyClass::DoAction(int id)
 56 {
 57     if(id == 0)
 58     {
 59         memcpy(name, "Apple", 6);
 60     }
 61     else
 62     {
 63         memcpy(name, "meat", 5);
 64     }
 65 
 66     this->id = 0xABCDEF;
 67 
 68 }
 69 
 70
 71 int main(void)
 72 {
 73     IMyClass * pClass = new CMyClass;
 74     int classSize = sizeof(CMyClass);
 75     pClass->DoAction(0);
 76     delete pClass;
 77     pClass = 0;
 78 
 79     CMyClass myclass;
 80     pClass = &myclass;
 81     pClass->DoAction(0);
 82     return 0;
 83 }

在函数 CMyClass::DoAction()里设置断点,在执行完这个函数最后一个赋值语句的时候,停下来。
此时,执行gdb命令:

(gdb) where
#0  CMyClass::DoAction (this=0x11008, id=0) at test_class.cpp:68
#1  0x00008b38 in main () at test_class.cpp:75
(gdb) p *this
$1 = {<IMyClass> = {_vptr.IMyClass = 0x8ce0 <vtable for CMyClass+8>, 
    id = 11259375, name = "Apple\000\000\000\000\000\000\000\000\000\000"}, 
  pFox = 0x11028}

看一下这时的mapping:

(gdb) info proc  mappings 
process 544
Mapped address spaces:

	Start Addr   End Addr       Size     Offset objfile
	    0x8000     0x9000     0x1000        0x0 /mnt/test_class
	   0x10000    0x11000     0x1000        0x0 /mnt/test_class
	   0x11000    0x32000    0x21000        0x0 [heap]
	0xb6d39000 0xb6e64000   0x12b000        0x0 /lib/libc-2.19.so
	0xb6e64000 0xb6e6c000     0x8000   0x12b000 /lib/libc-2.19.so
	0xb6e6c000 0xb6e6e000     0x2000   0x12b000 /lib/libc-2.19.so
	0xb6e6e000 0xb6e6f000     0x1000   0x12d000 /lib/libc-2.19.so
	0xb6e6f000 0xb6e72000     0x3000        0x0 
	0xb6e72000 0xb6e8f000    0x1d000        0x0 /lib/libgcc_s.so.1
	0xb6e8f000 0xb6e97000     0x8000    0x1d000 /lib/libgcc_s.so.1
	0xb6e97000 0xb6e98000     0x1000    0x1d000 /lib/libgcc_s.so.1
	0xb6e98000 0xb6f02000    0x6a000        0x0 /lib/libm-2.19.so
	0xb6f02000 0xb6f09000     0x7000    0x6a000 /lib/libm-2.19.so
	0xb6f09000 0xb6f0a000     0x1000    0x69000 /lib/libm-2.19.so
	0xb6f0a000 0xb6f0b000     0x1000    0x6a000 /lib/libm-2.19.so
	0xb6f0b000 0xb6fc4000    0xb9000        0x0 /lib/libstdc++.so.6.0.18
	0xb6fc4000 0xb6fcb000     0x7000    0xb9000 /lib/libstdc++.so.6.0.18
	0xb6fcb000 0xb6fcf000     0x4000    0xb8000 /lib/libstdc++.so.6.0.18
	0xb6fcf000 0xb6fd1000     0x2000    0xbc000 /lib/libstdc++.so.6.0.18
---Type <return> to continue, or q <return> to quit---
	0xb6fd1000 0xb6fd7000     0x6000        0x0 
	0xb6fd7000 0xb6ff6000    0x1f000        0x0 /lib/ld-2.19.so
	0xb6ffa000 0xb6ffd000     0x3000        0x0 
	0xb6ffd000 0xb6ffe000     0x1000        0x0 [sigpage]
	0xb6ffe000 0xb6fff000     0x1000    0x1f000 /lib/ld-2.19.so
	0xb6fff000 0xb7000000     0x1000    0x20000 /lib/ld-2.19.so
	0xbefdf000 0xbf000000    0x21000        0x0 [stack]
	0xffff0000 0xffff1000     0x1000        0x0 [vectors]
可以看出,this是在heap里的。

再看下成员变量的地址:

(gdb) p &name
$4 = (char (*)[16]) 0x11010
(gdb) p pFox
$5 = (CFox *) 0x11028
(gdb) p &this->id
$6 = (int *) 0x1100c
然后,看下this 所在的 heap的内存:

(gdb) x /20w this
0x11008:	0x00008ce0	0x00abcdef	0x6c707041	0x00000065
0x11018:	0x00000000	0x00000000	0x00011028	0x00000011
0x11028:	0x0000000a	0x00000000	0x00000000	0x00020fd1
0x11038:	0x00000000	0x00000000	0x00000000	0x00000000
0x11048:	0x00000000	0x00000000	0x00000000	0x00000000


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值