Skip to content

Instantly share code, notes, and snippets.

@brianm
Created January 25, 2012 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianm/1678117 to your computer and use it in GitHub Desktop.
Save brianm/1678117 to your computer and use it in GitHub Desktop.
a fast memory leak
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
int main(int argc, char *argv[]) {
size_t i;
void *mem, *prev;
mem = NULL;
for (i = 0; i < SIZE_MAX; i++) {
if ( (i % 1024) == 0) printf("allocated for %zuM\n", i);
prev = mem;
mem = malloc(1048576);
if (mem == NULL) {
free(prev);
printf("Failed allocation at %zuM\n", i);
return 1;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment