2010年1月27日 星期三

Bad Memory Allocation in C++: size is too large

#include
#include
using namespace std;

void test(int *a) {

for(int i =0; i < 10; i++)
std::cout << "i = " << i
<< "\t a[i] = " << a[i]
<< std::endl;

}

int main(int argc, char ** argv) {

//int *a = new int[10];
int *a;

try {
a = new int[3000000000];
}
catch(bad_alloc&)
{
cout << "Error allocating memory." << endl;
}
catch (...) {
cout << "Error ." << endl;
}

for(int i =0; i < 10; i++)
a[i] = i;

(void)test(a);

delete [] a;

return 0;

}

沒有留言: