0

I checked this in both VS and CodeBlocks. CodeBlocks showed me the same address over and over but in VS the address changed every time I ran my program.

    int main()
{
    int x, * p;
    x = 5;
    p = &x;
    cout<<"the address of "<< *p <<" is : " << p << endl;

}
sun tea
  • 13
  • 2
  • See [this question](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) for more details about heap allocation. The exact address is not important due to virtual memory, but also should not be relied on to be repeatable as that is implementation defined. – Cory Kramer Jun 07 '21 at 14:26
  • Also [see here](https://stackoverflow.com/questions/1212797/how-is-heap-and-stack-memories-managed-implemented-allocated) – Cory Kramer Jun 07 '21 at 14:37
  • Where `p` points to `i` and `i` is a non-static local, in practice `p` points to a place on the stack. So, I'm not sure how reading about heap allocation will be of use. I take it that they're asking out of curiosity and not planning on relying on it being stable between runs. – timemage Jun 07 '21 at 15:06

1 Answers1

0

Wild guess: one is enabling address layout randomization and the other is not. Check the build options in both for an option that enables and disables it. For Microsoft's compiler this appears to be /DYNAMICBASE Assuming it was enabled and you disable it, you may see consistent results.

timemage
  • 536
  • 1
  • 4
  • 12