new
new不需要进行断言,操作系统会进行抛出异常或者有回调函数进行回调
void my_fun(void) //回调函数
{
cout<<"This is bad alloc fun."<<endl;
exit(1);
}
int main()
{
set_new_handler(my_fun);
int* p = new int[536870911];
cout << "Memory OK." << endl;
}
int main()
{
//抛出异常
try
{
int* p = new int[536870911];
//int* p = new int[51];
//....................
cout<<"Memory OK."<<endl;
}
catch(bad_alloc &e)
{
cout<<"Out Of Memory."<<endl;
}
return 0;
}