A program uses different regions of memory for static objects, automatic objects, and dynamically allocated objects

Different regions have very different addresses. If they were in the same region, they would have similar addresses. Better example, where we allocate 2 objects in each region:

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

int main (void)
{
  int stack1;
  int stack2;
  static int bss1;
  static int bss2;
  static int data1=1;
  static int data2=1;
  int* heap1 = malloc(1);
  int* heap2 = malloc(1);  
  char* rodata1 = "hello";
  char* rodata2 = "world";

  printf(".stack\t%p %p\n",  &stack1,  &stack2);
  printf(".bss\t%p %p\n",    &bss1,    &bss2);
  printf(".data\t%p %p\n",   &data1,   &data2);
  printf(".heap\t%p %p\n",   heap1,    heap2);
  printf(".rodata\t%p %p\n", rodata1,  rodata2);

  free(heap1);
  free(heap2);
}

Output (for example):

.stack  000000000022FE2C 000000000022FE28
.bss    0000000000407030 0000000000407034
.data   0000000000403010 0000000000403014
.heap   0000000000477C50 0000000000477C70
.rodata 0000000000404000 0000000000404006

As you can see the two variables in the same segment have nearly identical addresses, the only difference being the size of the objects (and possibly some room for alignment). While they have very different addresses compared to variables in the other segments.

Leave a Comment