Created
June 15, 2012 15:38
-
-
Save sacha-senchuk/2937088 to your computer and use it in GitHub Desktop.
Safe memory allocation in C
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
void * safe_malloc(size_t const size) | |
{ | |
void * p = malloc(size); | |
if (p == NULL) { | |
fprintf(stderr, "Out of memory\n"); | |
exit(1); | |
} | |
return p; | |
} | |
void * safe_realloc(void * p, size_t const size) | |
{ | |
p = realloc(p, size); | |
if (p == NULL) { | |
fprintf(stderr, "Out of memory\n"); | |
exit(1); | |
} | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is something that simply ensures that the allocation was done, and terminates the process otherwise.