Skip to content

Instantly share code, notes, and snippets.

@rightson
Created January 10, 2018 13:05
Show Gist options
  • Save rightson/b16d598a9cfe2fb46fb86ce8b69862ba to your computer and use it in GitHub Desktop.
Save rightson/b16d598a9cfe2fb46fb86ce8b69862ba to your computer and use it in GitHub Desktop.
Example memory layout
#include <stdio.h>
#include <stdlib.h>
int init_global_var = 10; /* Initialized global variable */
int global_var; /* Uninitialized global variable */
static int init_static_var = 20; /* Initialized static variable in global scope */
static int static_var; /* Uninitialized static variable in global scope */
int main(int argc, char **argv, char **envp)
{
static int init_static_local_var = 30; /* Initialized static local variable */
static int static_local_var; /* Uninitialized static local variable */
int init_local_var = 40; /* Initialized local variable */
int local_var; /* Uninitialized local variable */
char *dynamic_var = (char*)malloc(100); /* Dynamic variable */
printf("Address of initialized global variable: %p\n", &init_global_var);
printf("Address of uninitialized global variable: %p\n", &global_var);
printf("Address of initialized static variable in global scope: %p\n", &init_static_var);
printf("Address of uninitialized static variable in global scope: %p\n", &static_var);
printf("Address of initialized static variable in local scope: %p\n", &init_static_local_var);
printf("Address of uninitialized static variable in local scope: %p\n", &static_local_var);
printf("Address of initialized local variable: %p\n", &init_local_var);
printf("Address of uninitialized local variable: %p\n", &local_var);
printf("Address of dynamic variable: %p\n", dynamic_var);
printf("Address of function (code): %p\n", &main);
printf("Address of argc variable: %p\n", &argc);
printf("Address of argv variable: %p\n", &argv[0]);
printf("Address of environment variable: %p\n", &envp[0]);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment