Why does this return different values ​​every time I run the program? 0x3759F8B0 - 0x100

This is not a problem for me, but I just started thinking about it and I thought I would ask. Why would it return different values ​​every time I run the program (0x3759F8B0 - 0x100)?

One day he says 00AFFD00 and the next one says 006FFD48

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

      

+3


source to share


1 answer


I suppose your complete program source reads like

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

      



As @pat mentioned in the comment, your program emits the address of a variable test

, not its value. Modern operating systems have something called "address space layout randomization" (ASLR, see https://en.wikipedia.org/wiki/Address_space_layout_randomization for a good overview) that helps make it harder to exploit security vulnerabilities that can exist in the program. The idea is that each time the program is started, the addresses of the material used are randomized. Therefore, the address of the variables will change each time you run with ASLR enabled.

ASLR is now a standard feature on major operating systems. However, it can be disabled (not recommended) and without ASLR the above program would indeed always produce the same result.

+8


source







All Articles