-1

I wrote a real simple hello world application and defined one variable, however the address of variable(&i) is different every time I start the application. May I know the reason for this?

int i = 23444;
int b = 0;
std::cout << &i;
std::cout << "Hello World!\n"; 
std::cin >> b;
std::cout << i;
Greg
  • 36
  • 5
  • 3
    "Every time I board a plane, I get different seat assigned". Memory addresses are like plane seats. – el.pescado - нет войне Sep 25 '18 at 07:14
  • 1
    @Greg Perhaps you can clarify if the variables are local or file scope ones? – Lundin Sep 25 '18 at 09:58
  • A question discussing memory randomization might share some commonality with this question but it assumes a lot more domain knowledge -- the person asking a question about memory randomization allready knows what it is , this question is addressed to someone who has just encountered it for the first time. For this reason I wouldn't consider it a duplicate. – Hassan Syed Sep 25 '18 at 11:33
  • @Lundin I just put these code in a main function. Is it a local variable? – Greg Sep 28 '18 at 03:13

1 Answers1

1

The address of the variable is relative to the stack of the calling thread, which is allocated dynamically when the thread is created. So the program's stack(s) get different base memory address(es) each time the program is run.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Which in turn is because of ASLR. – Lundin Sep 25 '18 at 07:31
  • 1
    @lun: ASLR affects the load address of modules. The stack is not something that gets loaded into your address space. It gets allocated by the system, so ASLR is not involved here. – IInspectable Sep 25 '18 at 07:35
  • @IInspectable It's not obvious from the question if these variables are allocated on the stack or in .data/.bss. – Lundin Sep 25 '18 at 09:21
  • @lun: ASLR in Windows is randomized at boot time. If the variable's storage were in a module section, it would retain its address across runs, until the next boot. Regardless of that, I was responding to your comment suggesting, that stack space randomization were due to ASLR, which it isn't. – IInspectable Sep 25 '18 at 09:34
  • @IInspectable A heap address is due to ASLR though, isn't it? Ignoring the nature of the heap itself which will give different addresses depending on what else is already there. – Lundin Sep 25 '18 at 09:36
  • @lun: No, it isn't. ASLR in Windows is *strictly* about loading modules. – IInspectable Sep 25 '18 at 09:47
  • Even with ASLR, keep in mind that a module may not always be loaded at the same address every time it is loaded. A module does have a preferred load address, but the OS is free to use a different address if the preferred address is not available – Remy Lebeau Sep 25 '18 at 15:20
  • @RemyLebeau Thanks for your answer. However I would like to know since all the process is run on the virtual memory address and program have the same code. Is there any other condition may lead to load the module in different address? – Greg Sep 27 '18 at 01:51