Skip to content

Instantly share code, notes, and snippets.

@pjvandehaar
Last active August 29, 2015 14:16
Show Gist options
  • Save pjvandehaar/0dcea0a753c857aebb2b to your computer and use it in GitHub Desktop.
Save pjvandehaar/0dcea0a753c857aebb2b to your computer and use it in GitHub Desktop.
//investigate the memory layout of a c++ program
//run with: `gcc b.c && ./a.out $(seq 1000) | sort | uniq`
#define OUT(var) printf("%16lx %s\n", (unsigned long int) &var, #var )
#define NL printf("\n")
#include <stdio.h>
#include <stdlib.h>
char global_const = 5;
void f(char f_param) {
static char f_static = 5;
char f_local = 5;
OUT(f_param);OUT(f_local);OUT(f_static);NL;
if (f_param > 0) f(f_param - 1);
}
int main(int argc, char** argv) {
char main_static = 3;
void *heap = malloc(1);
void *heap2_big = malloc(0x1000000);
void *heap3 = malloc(1);
OUT(argc);OUT(argv);OUT(*argv);OUT(**argv);NL;
OUT(printf);OUT(fprintf);OUT(scanf);
OUT(malloc);OUT(free);OUT(abort);NL;
OUT(f);OUT(main);
OUT(global_const);NL;
OUT(main_static);
OUT(heap);OUT(*heap);OUT(*heap2_big);OUT(*heap3);NL;
f(main_static);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment