What if malloc fails?

前端 未结 10 562
轮回少年
轮回少年 2020-12-09 08:57

If a malloc allocation fails, should we try it again?

In something like this:

char* mystrdup(const char *s)  
{
    char *ab = NULL;

           


        
相关标签:
10条回答
  • 2020-12-09 09:17

    In my experience (UNIX) it is MOST important to handle malloc failures robustly immediately following a network administrators boast that the "new server you wanted is already out there and ready to go". To which a typical reply is something like: "Wow, that was fast, thanks". Its at THIS point that malloc will return a NULL.

    Best thing to do is throw a std::bad_alloc exception with a static error message which gets caught & displayed in main(). Hopefully your destructors do enough cleanup freeing memory that the error processing in main does not fail.

    If you use a reasonable OS and programming style, an OOM condition is recoverable.

    0 讨论(0)
  • 2020-12-09 09:18

    In a single-threaded program "trying again" without freeing any memory between tries make no practical sense. It will just loop forever.

    In a multi-threaded program this might "work", if another thread running in parallel suddenly decides to free some of its own memory. The loop in such case would constitute a classic "busy waiting" loop. But even in this case such code has very little practical value for more reasons than one.

    0 讨论(0)
  • 2020-12-09 09:18

    No, never. If malloc returns NULL, that indicates an error, and you should probably abort.

    0 讨论(0)
  • 2020-12-09 09:19

    It is incredibly unlikely that this will do what you want; if you're out of memory, busy-looping until you get more is likely to be disappointing. You should just return the NULL to the calling program so that it can deal with resource exhaustion, either by releasing memory it no longer needs or by returning an error.

    0 讨论(0)
  • 2020-12-09 09:19

    malloc() tries its best to allocate memory. If it fails, instead of re-trying to allocate memory in a while loop(the program might get stuck there forever), try freeing some memory held by some other process or thread, if you can and then retry.

    Another alternative would be increasing memory, by increasing swap file or paging memory, on the fly, from within the code itself(but its dangerous and not preferable) or doing it manually.

    Best way to avoid such problems is to calculate or estimate the memory requirement, while writing the code itself.

    0 讨论(0)
  • 2020-12-09 09:22

    In general, a modern malloc() implementation will return NULL only as an absolute last resort, and trying again will definitely not help. The only thing that will help is freeing some memory and then trying again. If your application holds any expendable resources, this would be the time to free them, and then give it another shot.

    In some environments, a useful practice is to allocate a small amount of memory as a rainy-day fund. If malloc() ever does return NULL, you can free that rainy-day fund, and then allocate whatever resources you need to be able to handle the error and exit gracefully. This was a common practice when programming with the old Macintosh Toolbox; if malloc() returned NULL, you could use that space to create a dialog to report the problem before exiting.

    0 讨论(0)
提交回复
热议问题