简体   繁体   中英

Why does this return different values everytime I run the program? 0x3759F8B0 - 0x100

It's not really a problem for me but I just started thinking about it and I thought I'd ask. Why would that return different values each time I run the program (0x3759F8B0 - 0x100)?

One time it says 00AFFD00 and the next it says 006FFD48

test = 0x3759F8B0 - 0x100; 
cout << &test << endl;

I suppose your full program source reads as

#include <iostream>
using namespace std;
int main()
{
    int test;
    test = 0x3759F8B0 - 0x100; 
    cout << &test << endl;
}

As @pat already mentioned in comment, your program emits the address of the variable test , not its value. On modern operating systems there is something called "address space layout randomization" (ASLR, see https://en.wikipedia.org/wiki/Address_space_layout_randomization for a good overview) which helps making it harder to exploit security vulnerabilities that may exist in a program. The idea is, that with every new start of a program the addresses of the stuff it uses are randomized. Hence the address of variables will change on every launch with ASLR enabled.

ASLR is now a standard feature in mainstream operating systems. However it can be disabled (not recommended) and without ASLR the above program would indeed always emit the same output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM