Can I rely on malloc returning NULL?

后端 未结 4 912
独厮守ぢ
独厮守ぢ 2020-11-27 22:19

I read that on Unix systems, malloc can return a non-NULL pointer even if the memory is not actually available, and trying to use the memory later on will trigg

4条回答
  •  执笔经年
    2020-11-27 23:00

    To view this from an alternative point of view:

    "malloc can return a non-NULL pointer even if the memory is not actually available" does not mean that it always returns non-NULL. There might (and will) be cases where NULL is returned (as others already said), so this check is necessary nevertheless.

提交回复
热议问题