为什么同一个变量在不同的运行中获得不同的虚拟地址?

发布于 2024-11-16 23:54:35 字数 1235 浏览 1 评论 0原文

我需要知道项目中变量(特别是堆变量)的虚拟地址。变量的指针值实际上就是它的虚拟地址。我的理解是,同一变量的虚拟地址在不同的运行中应该相同。我写了下面的简单代码来证明我的想法,但事实证明是错误的,

#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代码,每个进程只是生成完全相同的变量。而且不同进程的同一个变量有不同的指针值,这并不像我预期的那样。

谁能给我解释一下吗? 谢谢,

I need to know the virtual address of the variables (the heap variable, in particular) in my project. The pointer value of the variable is actually its virtual address. My understanding is that the Virtual Address of the same variable should the same at different runs. I wrote the following simple code to prove my thoughts, but it turns out to be wrong,

#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;
}

But I got different pointer values for different runs as follows,

-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 

I also wrote a simple MPI code that each process simply generates exactly the same variable. And the same variable for different processes have the different pointer values, which is not as I expected.

Can any one explain this to me ?
Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-11-23 23:54:35

无法保证变量的地址在运行之间保持不变。现代操作系统尝试更改程序加载的地址以及堆和堆栈的地址,以防止这种情况发生。他们这样做是因为这使得攻击者更难利用某些类别的错误。

具体来说,在您的情况下,您正在查看的变量是在堆上分配的指针。绝对没有理由期望在堆上分配两次内存会产生两次相同的地址。

There's no guarantee that a variable's address would stay the same between runs. Modern operating systems try to change the address programs are loaded into and the heap and stack's addresses, exactly to prevent that from happening. They do this because it makes it harder for attackers to exploit certain classes of bugs.

Specifically, in your case, the variable you're looking at is a pointer allocated on the heap. There's absolutely no reason to expect that allocating memory twice on the heap would produce the same address twice.

我的奇迹 2024-11-23 23:54:35

这个问题与我的一个问题类似:Linux下的伪随机堆栈指针?

正如 awser 中的链接所描述的那样,这一切都是出于安全原因而完成的。

This question is similar to one of mine: Pseudo-random stack pointer under Linux?

It's all done for sercurity reasons, as the link in the awser describes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文