DavidB
April 5th, 2006, 04:57 PM
Hello.
After having used Visual C++ version 6 for a number of years, and checking for new returning NULL, I am now starting to code with try-catch blocks.
(I know about the STL and <vector> class, but I’d like to master the try-catch approach too, so please bear with me).
I am wondering: what exactly is thrown with a bad_alloc exception? Is it a memory address? An integer? . . .
The reason I ask is because I am trying to write a small program that dynamically creates several arrays. All the examples I have found so far show it to handle an exception when allocating a single array. However, I am finding it tricky to code for multiple arrays. Here’s a code snippet for the try block (say, two 2D arrays, and one hundred 1D arrays):
try { // Beginning of try block
b1 = new double[mDim];
A = new double*[mDim]; //Allocate space for pointers to rows of A matrix
for (i = 0; i < mDim; i++) {
A[i] = new double[mDim]; //Allocate space for columns of A matrix
}//End for i
b2 = new double[mDim];
b3 = new double[mDim];
b4 = new double[mDim];
D = new double*[mDim]; //Allocate space for pointers to rows of D matrix
for (i = 0; i < mDim; i++) {
D[i] = new double[mDim]; //Allocate space for columns of D matrix
}//End for i
b5 = new double[mDim];
. . .
b100 = new double[mDim];
} // End of try block
How do I code the catch block? If a bad_alloc exception is thrown, I would have to deallocate all arrays created up to that point, wouldn’t I? Something similar to the following might have to be done (assumes all pointers initialized to NULL when first defined):
catch (bad_alloc& xa) { // Catch block, for allocation exceptions
cerr << "In catch block, so an allocation failed.\n”;
if (b1) delete[] b1;
if (A)
{//count down from the highest value of i that we reached before the exception
for (; i > 0; --i) {
delete[] A[i];
}
delete[] A;
}
if (b2) delete[] b2;
if (b3) delete[] b3;
if (b4) delete[] b4;
if (D)
{//count down from the highest value of i that we reached before the exception
for (; i > 0; --i) {
delete[] D[i];
}
delete[] A;
}
if (b5) delete[] b5;
. . .
if (b100) delete[] b100;
return 0;
} // End of catch block
This approach would get very tedious, especially if many more arrays are involved. If bad_alloc was an integer, and the deallocation sequence was ordered properly, the try block could use a switch statement to indicate which array caused the exception to be thrown, and then drop through the rest of the switch statements. Can this be done?
Your help is much appreciated.
David
After having used Visual C++ version 6 for a number of years, and checking for new returning NULL, I am now starting to code with try-catch blocks.
(I know about the STL and <vector> class, but I’d like to master the try-catch approach too, so please bear with me).
I am wondering: what exactly is thrown with a bad_alloc exception? Is it a memory address? An integer? . . .
The reason I ask is because I am trying to write a small program that dynamically creates several arrays. All the examples I have found so far show it to handle an exception when allocating a single array. However, I am finding it tricky to code for multiple arrays. Here’s a code snippet for the try block (say, two 2D arrays, and one hundred 1D arrays):
try { // Beginning of try block
b1 = new double[mDim];
A = new double*[mDim]; //Allocate space for pointers to rows of A matrix
for (i = 0; i < mDim; i++) {
A[i] = new double[mDim]; //Allocate space for columns of A matrix
}//End for i
b2 = new double[mDim];
b3 = new double[mDim];
b4 = new double[mDim];
D = new double*[mDim]; //Allocate space for pointers to rows of D matrix
for (i = 0; i < mDim; i++) {
D[i] = new double[mDim]; //Allocate space for columns of D matrix
}//End for i
b5 = new double[mDim];
. . .
b100 = new double[mDim];
} // End of try block
How do I code the catch block? If a bad_alloc exception is thrown, I would have to deallocate all arrays created up to that point, wouldn’t I? Something similar to the following might have to be done (assumes all pointers initialized to NULL when first defined):
catch (bad_alloc& xa) { // Catch block, for allocation exceptions
cerr << "In catch block, so an allocation failed.\n”;
if (b1) delete[] b1;
if (A)
{//count down from the highest value of i that we reached before the exception
for (; i > 0; --i) {
delete[] A[i];
}
delete[] A;
}
if (b2) delete[] b2;
if (b3) delete[] b3;
if (b4) delete[] b4;
if (D)
{//count down from the highest value of i that we reached before the exception
for (; i > 0; --i) {
delete[] D[i];
}
delete[] A;
}
if (b5) delete[] b5;
. . .
if (b100) delete[] b100;
return 0;
} // End of catch block
This approach would get very tedious, especially if many more arrays are involved. If bad_alloc was an integer, and the deallocation sequence was ordered properly, the try block could use a switch statement to indicate which array caused the exception to be thrown, and then drop through the rest of the switch statements. Can this be done?
Your help is much appreciated.
David