new没有抛出异常(vc6.0 and xp)
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
try{
char *pbigdataarray = new char[10000000000000000000ul];
if(pbigdataarray==NULL)
cout<<"wrong"<<endl;
}
catch(bad_alloc a)
{
cout<<"jfjl"<<endl;
}
catch(...)
{
cout<<"cdfc"<<endl;
}
return 0;
}
/***************/
解答:
在未标准化时,c++中的new运算符总是返回NULL表示分配失败,就像malloc那样。标准C++修订了new的语义,plain new在失败后抛出标准异常std::bad_alloc而不是返回NULL。故,采用的可能是未标准化的c++。
本文探讨了C++中new运算符在内存分配失败时的行为变化。早期版本中new会返回NULL,而在标准C++中则改为抛出std::bad_alloc异常。通过示例代码展示了如何捕获这些异常。

被折叠的 条评论
为什么被折叠?



