static int arr[10] memory address always ends in 060

linuxlmao :

I have a c program that looks like this

main.c

#include <stdio.h>
#define SOME_VAR 10

static int heap[SOME_VAR];


int main(void) {
    printf("%p", heap);
    return 0;
}

and outputs this when I run the compiled program a few times

0x58aa7c49060
0x56555644060
0x2f8d1f8e060
0x92f58280060
0x59551c53060
0xd474ed6e060
0x767c4561060
0xf515aeda060
0xbe62367e060

Why does it always end in 060? And is the array stored in heap?

Edit: I am on Linux and I have ASLR on. I compiled the program using gcc

Ctx :

The addresses differ because of ASLR (Address space layout ramdomization). Using this, the binary can be mapped at different locations in the virtual address space.

The variable heap is - in contrast to it's name - not located on the heap, but on the bss. The offset in the address space is therefore constant.

Pages are mapped at page granularity, which is 4096 bytes (hex: 0x1000) on many platforms. This is the reason, why the last three hex digits of the address is the same.

When you did the same with a stack variable, the address could even vary in the last digits on some platforms (namely linux with recent kernels), because the stack is not only mapped somewhere else but also receives a random offset on startup.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29331&siteId=1