C++ new默认申请空间失败是抛出异常的,而malloc申请失败默认是返回空指针
方法1、
try catch捕获异常
try
{
int *p = new int[10000000000];
}
catch (bad_alloc &e)
{
cout << e.what() << endl;
}
方法2
强制new不抛出异常,这时候会返回空指针
int *p = new (std::nothrow) int[1000000000000000000];
if (p == 0)
cout << "bad alloc" << endl;