C heap address changes between runs while other addresses remain

The pile bothers me because I don’t understand who creates it, who supports it and who decides where it should be ... This test shows part of my puzzle:

Source:

#include <malloc.h>
#include <stdio.h>

int a;
int b = 5;

int * getMeAPointer() {
    int * e = malloc(4);
    *e = 5;
    return e;
}


void main() {
    a = 5;
    int c = 5;
    int * d = (int *) 0x405554;
    *d = 5;
    int * e = getMeAPointer();
    printf("Address of a located in .bss is %x\n", &a);
    printf("Address of b located in .data is %x\n", &b);
    printf("Address of c located in stack is %x\n", &c);
    printf("Address of d located in stack is %x\n", &d);
    printf("Address of *d located absolutely is %x\n", d);
    printf("Address of e located in stack is %x\n", &e);
    printf("Address of *e located on heap is %x\n", e);
    printf("Address of getMeAPointer() located in .text is %x\n", getMeAPointer);
    free(e);
}

Printout Examples:

Address of a located in .bss is 0x405068
Address of b located in .data is 0x402000
Address of c located in stack is 0x22ff1c
Address of d located in stack is 0x22ff18
Address of *d located absolutely is 0x405554
Address of e located in stack is 0x22ff14
Address of *e located on heap is 0x541738
Address of getMeAPointer() located in .text is 0x4013b0

Address of a located in .bss is 0x405068
Address of b located in .data is 0x402000
Address of c located in stack is 0x22ff1c
Address of d located in stack is 0x22ff18
Address of *d located absolutely is 0x405554
Address of e located in stack is 0x22ff14
Address of *e located on heap is 0x3a1738
Address of getMeAPointer() located in .text is 0x4013b0

Address of a located in .bss is 0x405068
Address of b located in .data is 0x402000
Address of c located in stack is 0x22ff1c
Address of d located in stack is 0x22ff18
Address of *d located absolutely is 0x405554
Address of e located in stack is 0x22ff14
Address of *e located on heap is 0x351738
Address of getMeAPointer() located in .text is 0x4013b0

....etc....

Now these are my problems:

  • Why is the heap moving and none of the other segments? This is on Windows 7 with MinGW, and this file was compiled using GCC without additional flags (I do not believe this is an example of randomizing the address space layout).

  • , ? , ( ), , , , RUNNABLE ( C) , // ?

  • ld? , , ( ​​), ?

  • , , , . C, , ( , , , , , , )... - - ?

Google , , !

+4
2

?

, , . , malloc(), , , . , , , .

, ?

.

[sic!]

. , , . ( , , - , .)

ld?

, . (, ld, , toolchain.)

[...] , ( ​​)

, , , "" " ". , , .

, , .

, . Windows, Unix syscalls brk() / sbrk().

, C-

. , - , , C. , , "" "" C. C , , .

+5

, , ..

  • ,

C () - , , , malloc(), . . , , . . . , "" , . , / , , .

0

Source: https://habr.com/ru/post/1530775/


All Articles