Is address of global variables the same for different runs of the program?

孤人 提交于 2021-02-05 05:28:25

问题


Consider the following code snippet

int i=10;
int main()
{
    cout<<&i;
}

Once an exe is generated for the program, will the output be the same for different runs of the program? Assume that the OS supports virtual memory

Edit:The ques is specific to global variables which are stored in data segment. Since this is the first global variable, should the address come out to be same or different?


回答1:


You always get the same addresses if ASLR is disabled. You get unpredictable addresses if ASLR is enabled.




回答2:


The virtual address will be whatever the linker decided. The physical address will vary with each load.




回答3:


Simple answer: It depends :-)

If your OS starts for a program always the same environment with a virtual memory range which looks always the same, the output should be always the same.

But if you run the same os on different hardware ( maybe with different amount of ram available ) it could be res ult in a different address but normally the address is also the same, independent of the hardware.

But you should NEVER expect that the result is the same! In a short: Don't think about the virtual or real address of data in your prog. This is under control from the compiler, the OS and maybe some libraries as well. So simply ignore it!




回答4:


Short answer: on a user-mode program running on a x86-64 machine: no, you shouldn't assume that for any reason ever.


Long answer: It might happen that the address is the same but that is absolutely not guaranteed (at least on a program running on a x86_64 OS and machine).

I read some confusion about virtual/physical memory and how come that an address is "random" so let me explain something at a high-level view:

Targeting a x86_64 architecture and OS (let's say Windows) you can't even assume that the operating system itself will load all its components into memory in the same physical locations (some exceptions for the old bootloader conventions at 0000:7C00H, I have no idea how that works in a UEFI environment).

After segmentation (used or not depends on the OS, usually Windows just sets some plain segments for usermode and kernelmode) is put in place, once you switch to protected mode (or long) you again have no control on how the OS manages the virtual memory mechanism which hides layers of complexity and MMU-related operations to give your process an address space of its own.

Plus there are security measures in place: the linker might decide the base address for your executable but in other cases when ASLR is activated the OS can move its modules and your executable around as it pleases for security purposes.

Conclusion: unless you're dealing with very low-level stuff (e.g. physical addresses or directly writing memory areas on an external device) you should absolutely not rely on the address of the variable being the same across different runs. There's no guarantee on that.



来源:https://stackoverflow.com/questions/24952585/is-address-of-global-variables-the-same-for-different-runs-of-the-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!