So I am doing a small project which involves using the malloc
, realloc
and calloc
functions from time to time. I understood that after every allocation I should be checking if it failed and it points to null like so:
int* arr = malloc(size*sizeof(int));
if (!arr)
{
printf("ERROR! Not enough memory!\n");
exit(1); // to exit the program immedietly.
}
and I was wondering if it is okay to create a method that will do just that for every kind of pointer like so:
void checkOutOfMemory(const void* p) //gets a pointer and exit program if points to null.
{
if (!p)
{
printf("ERROR! Out of memory!\n");
exit(1);
}
}
I mean, it works so far, but I started wondering if there are any special cases I didn't know about.