Always check malloc'ed memory?

后端 未结 10 923
滥情空心
滥情空心 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:55

    Depends on the platform. For instance, on Linux (by default) it does not make much sense to check for NULL:

    http://linux.die.net/man/3/malloc

    By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. This is a really bad bug. In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM killer.

提交回复
热议问题