#include <stdio.h> // for printf
#include <stdlib.h> // for malloc
#include <stdint.h> // for uint32_t

int g;

int main(int argc, const char *argv[])
{
   int j;
   int k;

   printf("&g : %p\n", &g); // Très différent des adresses de j et k
   printf("&j : %p\n", &j);

   printf("\ng in BSS section, j on the stack.\n");
   printf("-> Large difference in addresses.\n");
   printf("------------------------------------------\n\n");


   printf("&j : %p\n", &j);
   printf("&k : %p\n", &k);
   printf("\nj and k on the stack, close to each other:\n");

   printf("&j - &k: %ld\n", &j - &k); // Écart entre variables sur la pile
   printf("------------------------------------------\n\n");

   // Memory allocation on the heap
   int* px1 = malloc(100 * sizeof(int));
   printf("px1 : %p\n", px1);

   int* px2 = malloc(42 * sizeof(int));
   printf("px2 : %p\n", px2);

   printf("px1 and px2 on the heap. But not so close.\n");

   printf("px2 - px1: %ld\n", px2 - px1); // Écart entre variables sur le tas

   printf("------------------------------------------\n\n");

   printf("There is room between the heap and the BSS section.\n");
   printf("px1 - &g: %ld\n", px1 - &g); // Écart entre variables sur le tas et dans la section .data

   return 0;
}
