Always check malloc'ed memory?

后端 未结 10 876
滥情空心
滥情空心 2020-12-09 03:05

I often catch myself doing the following (in non-critical components):

some_small_struct *ptr=(some_small_struct *) malloc(sizeof(some_small_struct));
ptr-&g         


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

    Furthermore, what if handling the null pointer exacerbates the precarious situation even more??

    I do not see why it can exacerbate the situation.
    Anyway, when writing code for windows ptr->some_member will throw access violation so you will immediately see the problem, therefore I see no reason to check the return value, unless your program has some opportunity to free the memory. For platforms that do not handle null-pointers in a good way(throwing exception) it is dangerous to ignore such points.

    0 讨论(0)
  • 2020-12-09 03:35

    Assuming that you are running on a Linux/MaxOs/Windows or other virtual memory system, then... the only reason to check the return value from malloc is if you have a strategy for freeing up enough memory to allow the program to continue running. An informative message will help in diagnosing the problem, but only if your program caused the out-of-memory situation. Usually it is not your program and the only thing that your program can to do help is to exit as quickly as possible.

    assert(ptr != NULL);
    

    will do all of these things. My usual strategy is to have a layer around malloc that has this in it.

    void *my_malloc(size_t size)
    {
        void *ptr = malloc ( size );
        assert(ptr != NULL);
        return *ptr;
    }
    

    Then you call my_malloc instead of malloc. During development I use a memory allocation library that is conducive to debugging. After that if it runs out of memory - I get a message.

    0 讨论(0)
  • 2020-12-09 03:37

    In the case of C, it depends on the platform. If you are on an embedded platform with very little memory, you should alweays check, thouggh what you do if it does fail is more difficult to say. On a modern 32-bit OS with virtual memory, the system will probably become unresponsive and crash before it admits to running out of memory. In this case, the call to malloc never returns, so the utility of checking its value becomes moot.

    In the case of C++, you should be using new instead of malloc, in which case an exception will be raised on exhaustion, so there is no point in checking the return value.

    0 讨论(0)
  • 2020-12-09 03:40

    Allocations can fail for several reasons. What you do (and can do) about it depends in part on the allocation failure.

    Being truly out of memory is catastrophic. Unless you've made a careful plan for this, there's probably nothing you can do. (For example, you could have pre-allocated all the resources you'd need for an emergency save and shutdown.)

    But many allocation failures have nothing to do with being out of memory. Fragmentation can cause an allocation to fail because there's not enough contiguous space available even though there's plenty of memory free. The question specifically said a "small structure", so this is probably as bad as true out-of-memory condition. (But code is ever-changing. What's a small structure today might be a monster tomorrow. And if it's so small, do you really need memory from the heap or can you get it from the stack?)

    In a multi-threaded world, allocation failures are often transient conditions. Your modest allocation might fail this microsecond, but perhaps a memory-hogging thread is about to release a big buffer. So a recovery strategy might involve a delay and retry.

    How (and if) you handle allocation failure can also depend on the type of application. If you're writing a complex document editor, and a crash means losing user's work, then it's worth expending more effort to handle these failures. If your application is transactional, and each change is incrementally applied to persistent storage, then a crash is only a minor inconvenience to the user. Even so, logging should be considered. If you're application is routinely getting allocation failures, you probably have a bug, and you'll need the logs to know about it and track it down.

    Lastly, you have to think about testing. Allocation failures are rare, so the chance that recovery code has been exercised in your testing is vanishingly small--unless you've taken steps to ensure test coverage by artificially forcing failures. If you aren't going to test your recovery code, then it's probably not worth writing it.

    0 讨论(0)
  • 2020-12-09 03:43

    at the very least I would put an assert(ptr != NULL) in there so you get a meaningful error.

    0 讨论(0)
  • 2020-12-09 03:50

    I would say No. Using a NULL pointer is going to crash the program (probably).
    But detecting it and doing something intelligent will be OK and you may be able to recover from the low memory situation.

    If you are doing a big operation set some global error flag and start unwinding the stack and releasing resources. Hopefully one or more of these resources will be your memory hog and your application will get back to normal.

    This of course is a C problem and handeled automatically in C++ with the help of exceptions and RAII.
    As new will not return NULL there is no point in checking.

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