【发布时间】:2011-12-15 05:28:53
【问题描述】:
代码://VS2010
int a;
int b;
int c;
int d;
int main(){
//output the address of global variables
printf("0x%x, 0x%x, 0x%x, 0x%x\n", &a, &b, &c, &d);
int a1, b1, c1, d1;
//output the address of local variables
printf("0x%x, 0x%x, 0x%x, 0x%x\n", &a1, &b1, &c1, &d1);
int a2 = 1;
int b2 = 2; int c2; int d2 = 4;
//output the address of local variables
printf("0x%x, 0x%x, 0x%x, 0x%x\n", &a2, &b2, &c2, &d2);
}
输出:
0x1197a44, 0x1197a3c, 0x1197a40, 0x1197a38
0x15fb00, 0x15faf4, 0x15fae8, 0x15fadc
0x15fad0, 0x15fac4, 0x15fab8, 0x15faac
我的问题:
Why are the global variables not stored in order?上面的输出表示它们是乱序的。Why are the local varialbes not stored continuously?上面的输出代表VS2010每两个之间插入8字节的空间。
有人可以帮助我吗?非常感谢!
---------------------------补充-------- ----------------------------------
代码:\gcc 版本 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)
int a;
int b;
int c;
int d;
void main(){
//output the address of global variables
printf("%p, %p, %p, %p\n", &a, &b, &c, &d);
int a1, b1, c1, d1;
//output the address of local variables
printf("%p, %p, %p, %p\n", &a1, &b1, &c1, &d1);
int a2 = 1;
int b2 = 2; int c2; int d2 = 4;
//output the address of local variables
printf("%p, %p, %p, %p\n", &a2, &b2, &c2, &d2);
}
输出:
0x60103c, 0x601034, 0x601038, 0x601030
0x7fff126253a0, 0x7fff126253a4, 0x7fff126253a8, 0x7fff126253ac
0x7fff126253b0, 0x7fff126253b4, 0x7fff126253b8, 0x7fff126253bc
在gcc中,全局变量和局部变量的地址是连续有序的。
所以我想知道 vs2010 对我们的代码做了什么以及为什么这样做。
【问题讨论】:
标签: c++ c visual-studio-2010 gcc compiler-construction