I'm running the below block of code on my computer.
/* Example code for Think OS.
Copyright 2014 Allen Downey
License: GNU GPLv3
*/
#include <stdio.h>
#include <stdlib.h>
int var1;
int main ()
{
int var2 = 5;
void *p = malloc(128);
void *p2 = malloc(128);
char *s = "Hello, World";
printf ("Address of main is %p\n", main);
printf ("Address of var1 is %p\n", &var1);
printf ("Address of var2 is %p\n", &var2);
printf ("p points to %p\n", p);
printf ("p2 points to %p\n", p2);
printf ("s points to %p\n", s);
return 0;
}
Here is a table of the resulting addresses:
| Stack | var2 | 0x7fff58cf28d8 |
| Heap | p2 | 0x7fb4c2d032d0 |
| Heap | p | 0x7fb4c2d03250 |
| Globals | var1 | 0x106f0e020 |
| Constants | "Hello, World" | 0x106f0df34 |
| Code | main | 0x106f0de30 |
My mental model is that the heap should start right after the Globals section and grow up in address space. It grows up, but I don't understand the gap.
Why is there such a big gap between var1
and p
?
gdb doesn't have a great tool to visualize the entire address space (so if you have tools that could help I'm open to suggestions).