Why does my program occasionally segfault when out of memory rather than throwing std::bad_alloc?

风流意气都作罢 提交于 2019-12-01 03:09:41

One reason might be that by default Linux overcommits memory. Requesting memory from the kernel appears to work alright, but later on when you actually start using the memory the kernel notices "Oh crap, I'm running out of memory", invokes the out-of-memory (OOM) killer which selects some victim process and kills it.

For a description of this behavior, see http://lwn.net/Articles/104185/

It could be some code using no-throw new and not checking the return value.

Or some code could be catching the exception and not handling it or rethrowing it.

What janneb said. In fact Linux by default never throws std::bad_alloc (or returns NULL from malloc()).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!