Do malloc () return NULL instead of a program crash?

I allocate memory in a C program using malloc . Perhaps my program allocates more memory than the system has space, and at that moment the program will fail. For my purposes, it would be better if malloc just returned NULL (as if it should ), so I can catch the error. Instead, it displays the error message "There is no memory for programming now: it is not safe to call malloc." And the program crashes.

How can i fix this?

Edit: I know that a program crashes on its own, and not because I'm trying to reference a null pointer. A program never directly calls malloc , but instead calls a function that I wrote, which calls malloc , and then checks to see if it returns NULL . He never said that malloc returned NULL .

Edit 2: if this is useful, here is the complete error output:

Programmed signal: "EXC_BAD_ACCESS".
sharedlibrary apply-load-rules all
warning: it is not possible to restore a previously selected frame.
Data formats are temporarily unavailable, try again after continuing. (The program to be debugged was specified in a function called by GDB.
GDB remains in the frame where the signal was received.
To change this behavior, use "set unwindonsignal on"
The evaluation of the expression containing the function (dlopen) will be terminated.)
Now there is no memory for programming: it is not safe to call malloc

+4
source share
5 answers

As soon as you throw a bunch through buffer overflows, wild pointers or other errors, the malloc behavior becomes undefined and it can return something.

Malloc is just a user space library; there is no magic in it. If I scratch the linked list of customer names throughout your application, you will get strange behavior when you later access this list. Malloc behaves the same, but since the use of malloc is distributed through code, cause and effect have a global reach.

All answers dance around the fact that pointer errors are the single most common source of defects in C code. You are fortunate to get SIGBUS, which is evidence of a defect that can be widely separated from where and when the error occurs. Use valgrind to find where the real defect is.

+2
source

It checks if Malloc returns NULL? You may have a problem with the equality test. Try something like "if (malloc (...)) then ...; else ...; rather than a specific check.

If this does not help, just start the preprocessor and find out that NULL is "edited".

+1
source

According to this apple page , the program will be interrupted based on the malloc error if the MallocErrorAbort environment MallocErrorAbort included in xcode. To disable this variable, right-click the executable file in the "Tree" view, select "Get Information" and go to the "Arguments" tab .

+1
source

Strange ... for me it will work if I compile it from the command line. Runtime malloc has many options that Spencer talked about . If you are using Xcode, I would be looking for an option that controls this.

 #include <limits.h> #include <stdio.h> #include <stdlib.h> int main() { signed long long alloc_sz = (1 * 1024 * 1024 * 1024); while (alloc_sz > 0ull) { char *ptr = malloc(alloc_sz); if (ptr == NULL) { fprintf(stderr, "failed to allocate %llu bytes\n", (unsigned long long)alloc_sz); break; } else { free(ptr); fprintf(stderr, "allocated %llu bytes\n", (unsigned long long)alloc_sz); } alloc_sz *= 2ull; } return 0; } 

By the way: is it in a debug or release build? Maybe Xcode is trying to help you in some way.

+1
source

As others have said, it is very likely that you have rewritten some kind of malloc's internal state, forcing him to do strange things.

On Linux, as you could track this, it would use valgrind . If your code is cross-platform enough that you can run it on Linux (possibly in a virtual machine), I would recommend doing this.

If this option is not available to you, Mac OS X has its own built-in debugging tools. At a high level there are tools. At a lower level, malloc will enable debugging in response to certain environment variables: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/malloc.3.html Scroll down to the ENVIRONMENT section. So, for example, you would run your program with

 MallocGuardEdges=1 ./myProg 

You can also use libgmalloc , which is similar to cracking malloc debugging features. You can read about it here: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/libgmalloc.3.html

You run your program as follows:

 DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib ./myProg 
0
source

All Articles