为什么相同的变量在不同的运行中得到不同的虚拟地址?
|
我需要知道项目中变量(尤其是堆变量)的虚拟地址。变量的指针值实际上是其虚拟地址。我的理解是,同一变量的虚拟地址在不同运行时应相同。我编写了以下简单的代码来证明自己的想法,但事实证明这是错误的,
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int node=0;
char* buffer;
int i;
printf(\"Hello World from Node %d the vm of i is %p and %ld \\n\",node, &i,&i);
buffer = (char*)malloc(sizeof(char)*1024*1024*300);
for(i = 0; i < 1024*1024*300; i++){
buffer[i] = (char)1;
}
printf(\"I am in %d and the vm of buffer is %p:%ld \\n\",node, buffer,buffer);
return 0;
}
但是对于不同的运行,我得到了不同的指针值,如下所示:
-bash-4.0$ gcc singletest.c
-bash-4.0$ ./a.out
Hello World from Node 0 the vm of i is 0x7fffb87a8e00 and 140736288427520
I am in 0 and the vm of buffer is 0x7fb05dfcf010:140395467829264
-bash-4.0$ ./a.out
Hello World from Node 0 the vm of i is 0x7fffec2856f0 and 140737155454704
I am in 0 and the vm of buffer is 0x7f5888c54010:140018228477968
-bash-4.0$ ./a.out
Hello World from Node 0 the vm of i is 0x7fff1f44a780 and 140733717981056
I am in 0 and the vm of buffer is 0x7fbea3b91010:140456767328272
我还编写了一个简单的MPI代码,每个进程仅生成完全相同的变量。而且针对不同进程的相同变量具有不同的指针值,这与我预期的不同。
有人可以向我解释吗?
谢谢,
没有找到相关结果
已邀请:
2 个回复
坛沤疲撑拆
埃输林桨铃