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

后端 未结 8 784
别那么骄傲
别那么骄傲 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条回答
  •  旧时难觅i
    2020-12-29 19:26

    in C++, operator new is not only to allocate a chunk of memory, it is also class construction. thus, in semantic, operator new is much richer than function malloc. via exceptions, we get better informations and we can handle construction failures better.

提交回复
热议问题