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

前端 未结 18 1847
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  暖寄归人
    2021-01-30 09:22

    new operator will throw std::bad_alloc exception when there are not enough available memory in the pool to fulfill runtime request.

    This can happen on bad design or when memory allocated are not freed correctly.

    Handling of such exception is based on your design, one way will be pause and retry some time later, hoping more memory returned to the pool and the request may succeed.

提交回复
热议问题