【发布时间】:2011-11-25 04:33:32
【问题描述】:
有这样的代码:
#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++,一切正常。
【问题讨论】:
-
这个程序不应该编译。很遗憾,它确实如此。您应该添加
#include <new>。无论如何,您使用的是哪个版本的 VC++? -
嗯,确实如此。 2010 年的版本。
-
它也在 gcc 上编译。到底是什么让我相信实际运行它是个好主意?
-
@pmr:当然,在运行此程序之前,您会将
ulimit设置为较小的值:-) 顺便说一句,效果很好。 -
也许 return 而不是
break,会发生什么?
标签: c++ exception new-operator nothrow