Can the C++ `new` operator ever throw an exception in real life?

前端 未结 18 1848
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 08:40

Can the new operator throw an exception in real life?

And if so, do I have any options for handling such an exception apart from killing my application?

18条回答
  •  旧时难觅i
    2021-01-30 09:31

    new operator will throw std::bad_alloc exception when you run out of the memory ( virtual memory to be precise).

    If new throws an exception then it is a serious error:

    • More than available VM is getting allocated ( it fails eventually). You can try reducing the amount of memory than exiting the program by catching std::bad_alloc exception.

提交回复
热议问题