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

后端 未结 3 724
悲哀的现实
悲哀的现实 2021-01-11 16:32

I have a program that implements several heuristic search algorithms and several domains, designed to experimentally evaluate the various algorithms. The program is written

相关标签:
3条回答
  • 2021-01-11 16:47

    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.

    0 讨论(0)
  • 2021-01-11 16:47

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

    0 讨论(0)
  • 2021-01-11 16:56

    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/

    0 讨论(0)
提交回复
热议问题