想调试volatile的效果,在栈定义了a,b,i三个变量,在堆定义了p指针,还有全局变量g_a,按栈向下生长应该是 &i >&a>&b,但实际相反,代码和运行结果如下:
int *p = new int;
cout << "p = " << p << endl;
cout << "g_a address = " << &g_a << endl;
int i = 1;
int a = i;
cout << "a = " << a << endl;
cout << "a address = " << &a << endl;
cout << "i address = " << &i << endl;
// 下面汇编语句的作用就是改变内存中 i 的值
//但是又不让编译器知道
__asm {
mov dword ptr [ebp-4],0x20
};
int b = i;
cout << "b = " << b << endl;
cout << "b address = " << &b << endl;
cout << "i address = " << &i << endl;
delete(p);
查资料发现栈的生长方向和平台有关,例如使用STM32平台ARM内核可以看出栈的生长方向是向下如图,依次定义了i,a,b在watch窗口观测3个变量RAM的地址是递减