1

I wrote following sample piece of code, just to print the address of a local variable. Since the variable a is in a known stack frame, so I thought its address is fixed. But when I run the compiled binary multiple times, it print out different addresses. I wonder why the address of a keeps changing.

#include <stdio.h>

int main() {
  int a = 1;
  printf("address: %p\n", &a);
  return 0;
}
PeopleMoutainPeopleSea
  • 1,492
  • 1
  • 15
  • 24
  • 1
    See: https://unix.stackexchange.com/questions/559761/why-are-the-virtual-memory-regions-for-the-same-program-different-on-every-run/559775#559775 – kaylum Jan 07 '20 at 06:08
  • 6
    For security reasons. See [ASLR](https://en.wikipedia.org/wiki/Address_space_layout_randomization) – user3386109 Jan 07 '20 at 06:09

0 Answers0