C++:内存不足时会返回什么?

Cha*_*hin 0 c++ out-of-memory nothrow

当我编码如下时,它将返回'null'并且不会发生异常.

Char* pStr = new(std::nothrow)Char(10);
Run Code Online (Sandbox Code Playgroud)

那么不在new运算符上使用'nothrow'参数呢?它是否也返回'null'?如果是这样,为什么建议使用'nothrow'参数?

Char* pStr = new Char(10);
Run Code Online (Sandbox Code Playgroud)

谢谢你的时间.

Cor*_*lks 5

new如果失败,将抛出异常,除非您指定nothrow,在这种情况下,nullptr如果失败,它将返回.

至于为何nothrow使用过:在某些系统上,不支持异常(或者支持不当)(在游戏控制台上尤其如此).所以最好甚至根本不使用它们.这只是nothrow可以使用的一个例子.