Under what circumstances can malloc return NULL?

前端 未结 9 1714
陌清茗
陌清茗 2020-12-23 14:17

It has never happened to me, and I\'ve programming for years now.

Can someone give me an example of a non-trivial program in which malloc will actually

9条回答
  •  攒了一身酷
    2020-12-23 15:06

    Yes.

    Just try to malloc more memory than your system can provide (either by exhausting your address space, or virtual memory - whichever is smaller).

    malloc(SIZE_MAX)
    

    will probably do it. If not, repeat a few times until you run out.

提交回复
热议问题