Skip to content

Instantly share code, notes, and snippets.

@gebi
Last active December 16, 2015 18:59
Show Gist options
  • Save gebi/5481373 to your computer and use it in GitHub Desktop.
Save gebi/5481373 to your computer and use it in GitHub Desktop.
Short example program to show linux memory allocation behaviour (memory is only allocated on first access, in this example program after you press enter) Test with: gcc -O0 -Wall static_mem_test.c -o static_mem_test && /usr/bin/time --verbose ./static_mem_test
#include <unistd.h>
static char staticfoo[80*1024*1024]; // = {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"};
#define WRITE(Str) write(1, Str, sizeof(Str))
int main()
{
char in;
int i;
for(i=0; i<sizeof(staticfoo); i++) {
in = staticfoo[i];
if(in != 0)
return(1);
}
WRITE("pause...");
read(0, &in, 1);
for(i=0; i<sizeof(staticfoo); i++)
staticfoo[i] = 0;
WRITE("pause...");
read(0, &in, 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment