Is it required to check a pointer validity if new fails?

前端 未结 4 883
轮回少年
轮回少年 2021-01-15 21:29

As title says, i know that new throws an exception which can be caught, but what exactly happens to the pointer? it turns NULL? I checked some answers on SO but none explain

4条回答
  •  既然无缘
    2021-01-15 22:29

    If new fails it throws a std::bad_alloc exception, unless you use the nothrow version. This means that the value of the variable to store the returned pointer will be unchanged.

    If you catch the exception the value of pFile remains uninitialized, otherwise your application terminates (as main is exited)

提交回复
热议问题