Linux optimistic malloc: will new always throw when out of memory?

后端 未结 5 1663
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 05:04

I have been reading about out of memory conditions on Linux, and the following paragraph from the man pages got me thinking:

By default, Linux follows

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 05:24

    Forgive me if I'm wrong, but wouldn't trying to zero out the memory allocated be enough to guarantee that you have every single byte you requested? Or even just writing to the last byte, it would throw an exception if the memory wasn't really yours right?

    If that's true, you could just try writing to the last (and first?) byte of the memory and see if it works fine, and if it doesn't you could return null from malloc.

提交回复
热议问题