Always check malloc'ed memory?

后端 未结 10 917
滥情空心
滥情空心 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.

提交回复
热议问题