Guys, how should we handle cases when malloc() returns NULL?
I usually call a function which prints Memory allocation error and then calls exit(), assuming no pointers were lost in program so far. If so, this causes no memory leaks (detected by valgrind).
But there is "still reachable" memory segments left, which is not quite good. So we may create a total free function to call before exit(). Or do something else? We could make the user decide what to do, but we are limited in allowed system functions to use.
In my opinion we don't need to worry about still reachable blocks. Exit() do all the works for us.
While we free memory block right after we don't need it anymore we are in a safe zone.
Guys, how should we handle cases when
malloc()
returnsNULL
? I usually call a function which printsMemory allocation error
and then callsexit()
, assuming no pointers were lost in program so far. If so, this causes no memory leaks (detected by valgrind).But there is "still reachable" memory segments left, which is not quite good. So we may create a total free function to call before
exit()
. Or do something else? We could make the user decide what to do, but we are limited in allowed system functions to use.