Created
January 25, 2012 19:39
-
-
Save brianm/1678117 to your computer and use it in GitHub Desktop.
a fast memory leak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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