编译时间内存分配

Ati*_*hay 0 c++ compiler-construction

这似乎是一个非常基本的问题,但它与编译器的工作方式有关.什么是函数的局部变量的内存分配顺序.

假设我有一个功能

int a,b;
int c1;
int c,d;
int c2;
cout<<&a<<endl;
cout<<&b<<endl;
cout<<&c<<endl;
cout<<&d<<endl;
cout<<&c1<<endl;
cout<<&c2<<endl;
int f;
cout<<&f<<endl;
Run Code Online (Sandbox Code Playgroud)

这里f得到最低的内存地址(相对于其他变量),好像初始化堆栈是这样构建的:

a b c1 c d c2 f
Run Code Online (Sandbox Code Playgroud)

然后分配了内存

这可能发生,因为有各种编译阶段,这是其中之一.

该堆栈构建的哪个阶段对应于哪个阶段实际分配的内存?

sha*_*oth 6

无法保证这些变量将占据哪些位置 - 定义顺序仅影响构造函数/析构函数调用的顺序.