Why doesn't new in C++ return NULL on failure

后端 未结 8 780
别那么骄傲
别那么骄傲 2020-12-29 19:17

Why doesn\'t new return NULL on failure? Why does it throw an exception only on failure?

As it returns a pointer to the object on successes

8条回答
  •  粉色の甜心
    2020-12-29 19:23

    you can specify that you want new to return 0 instead of throwing std::bad_alloc using the std::nothrow parameter:

    SomeType *p = new(std::nothrow) SomeType;
    

提交回复
热议问题