4

Take this C code for example

#include <stdio.h>
#include <stdlib.h>

int main() {

    int x;
    int* y = (int *) malloc(10*sizeof(int));

    printf("%p\n",&x);
    printf("%p\n",y);
    printf("%p\n",&(y[1]));

    while(1);

    return 0;
}

Which will print virtual addresses that look something like this

0x7ffd4e96d214

0x908010

0x908014

The virtual addresses will be different every time you run the binary file which made me think how the virtual address are actually decided for a program ?

AGB
  • 2,230
  • 1
  • 14
  • 21
Udai F.mHd
  • 82
  • 1
  • 7
  • 1
    [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Apr 10 '16 at 04:32
  • Looks like Windows numbers. &x is an address on the stack, y is an address on the heap. Locations of a thread's stack and the heap in a x64 process are randomized by /DYNAMICBASE and /HIGHENTROPYVA, makes your program hard to attack. – Hans Passant Apr 10 '16 at 08:01
  • Thanks for the tip @LưuVĩnhPhúc – Udai F.mHd Apr 10 '16 at 15:11

1 Answers1

4

This is - probably - the effect of ASLR.

The decision should - as the name Address Space Layout Randomization tells - be random.

makadev
  • 1,034
  • 15
  • 26