Skip to content

Instantly share code, notes, and snippets.

@ruanchao
Forked from alexpirine/safealloc.c
Created July 20, 2012 15:02
Show Gist options
  • Save ruanchao/3151189 to your computer and use it in GitHub Desktop.
Save ruanchao/3151189 to your computer and use it in GitHub Desktop.
Safe memory allocation in C
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