我的类变量之一是二维数组。大小取决于用户输入。用户可以输入可能超过他的硬件限制的大小。所以我想妥善处理这个问题。下面的代码是否正确?
int counter;
try
{
int size = 20000;//this is actually from user input
array = new double*[size];
for(counter = 0; counter < size; counter++)
array[counter] = new double[size];
}
catch(std::bad_alloc)
{
try
{
for(int i = 0; i < counter; i++)
delete[] array([i]);
delete[] array;
array = NULL;
//display limitation message
size = 2;
array = new double*[size];
for(int i = 0; i < size; i++)
array[i] = new double[size];
}
//catch again & exit application
}