大致测试了下,堆区大概可以申请 1G这样。捕获 new 的异常,一般抛出 std::bad_alloc
代码语言:javascript复制void test(){
char * c = NULL;
// input bad_alloc
//unsigned int i = (unsigned int)pow(2,31);
// ok
unsigned int i = (unsigned int)pow(2,30);
try{
c = new char[i];
}catch(bad_alloc& e){
cout << e.what() << endl;
//cout << s << endl;
}
if( c != NULL)
delete []c;
}