总是检查malloc’ed记忆?

我经常碰到自己做以下(在非关键组件):

some_small_struct *ptr=(some_small_struct *) malloc(sizeof(some_small_struct));
ptr->some_member= ...;

换句话说,我为一个小的结构动态分配内存,我直接使用它,而不检查malloc的指针。我知道这个程序总是有机会得到它要求的内存(呃!),但是要考虑以下几点:

If the program can’t even get some memory for a small structure off the
heap, maybe there are much bigger problems looming and it doesn’t matter after all.

此外,如果处理空指针会加剧不稳定的情况呢? (例如,尝试登录条件调用更多的非现有资源等)

我的推理是否合理(足够)?

更新:

>“safe_malloc”功能在调试时非常有用,否则可能会有用
> X访问可以隐藏NULL指针的根本原因
>在Linux上,“乐观的内存分配”可以影响LOM OOM(Out of Memory)条件

取决于平台。例如,在Linux上(默认情况下),检查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.

http://stackoverflow.com/questions/1941323/always-check-malloced-memory

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:总是检查malloc’ed记忆?