I am allocating memory in a C program using malloc
. It's possible for my program to allocate more memory than the system has room for, at which point the program crashes. For my purposes it would be better if malloc
would just return NULL
(like it's apparently supposed to), so I can catch the error. Instead what it does is it throws an error saying "No memory available to program now: unsafe to call malloc." And crashes the program.
How can I fix this?
Edit: I know that the program is crashing by itself and not because I'm trying to reference a null pointer. The program never directly calls malloc
, but instead calls a function I wrote that calls malloc
and then checks to see if it returns NULL
. It's never saying that malloc
returned NULL
.
Edit 2: If it's helpful, here is the complete error output:
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
warning: Unable to restore previously selected frame.
Data Formatters temporarily unavailable, will re-try after a 'continue'. (The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on"
Evaluation of the expression containing the function (dlopen) will be abandoned.)
No memory available to program now: unsafe to call malloc