通过分析我们可以看到我们的3个全局变量global_const_string, global_int, global_static_int全都分布在422000 - 426000之间的.data可读写数据节中。

  而global_const_string所指向的内容

0x0041d1dc "hello world"

  则分布在41d000 -  422000 之间的.rdata只读数据节中,这个结论也符合我们平时关于全局变量存储区域的理解。

  下面我们再尝试分析局部变量的存储区域,再main函数内部cout的地方设置断点,然后让程序运行到此,然后输入dv /t /i /v命令查看所有局部变量,可以看到

0:000> dv /t /i /v
prv local  0042200c int local_static_int = 0n100
prv local  0013ff70 int local_int = 0n200
prv local  0013ff74 int * pValue = 0x02248ff8

  我们可以看到local_static_int也分布在422000 - 426000之间的.data可读写数据节中,而local_int和pValue则都存储在13e000 - 140000之间的堆栈区域上。

  而指针pValue所指向的地址0x02248ff8我们可以通过!address 0x02248ff8命令来分析,结果是:

0:000> !address 0x02248ff8

Usage:                  Heap
Allocation Base:        021d0000
Base Address:           02248000
End Address:            02249000
Region Size:            00001000
Type:                   00020000    MEM_PRIVATE
State:                  00001000    MEM_COMMIT
Protect:                00000004    PAGE_READWRITE
More info:              !heap -p 0x21d1000
More info:              !heap -p -a 0x2248ff8

  可以看到地址0x02248ff8是在堆(heap)上面。

  通过上面的分析,我们验证了平时C++书上关于各种类型变量存储区域的假设,简单来说是全局变量和静态变量会被编译到可执行文件的数据节(分只读和可读写)中,非静态的局部变量则分配在堆栈(stack)上,而new(malloc)出来的内存则分配在堆(heap)上。