gpt4 book ai didi

c++ - 带有 nothrow 选项的 Operator new 仍然抛出异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:32:33 26 4
gpt4 key购买 nike

有这样的代码:

#include <iostream>

int main(){
for(;;){
int* ptr = new (std::nothrow) int;
if(ptr == 0){
std::cout << 0 << std::endl;
break;
}
}
std::cin.get();
return 0;
}

然而,这个程序仍然抛出 std::bac_alloc 异常,尽管 new 是用 std::nothrow 参数调用的。该程序在Visual C++ 2010中编译,为什么会抛出异常?

编辑:

在 Windows 上从 mingw 使用 g++,一切正常。

最佳答案

0 必须格式化为 "0"。这将占用几个字节;我敢打赌这就是原因。在 std::bad_alloc::bad_alloc 上放置一个断点,你就会知道了。

关于c++ - 带有 nothrow 选项的 Operator new 仍然抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7555138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com