Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

behaviour of "new" on bad_alloc

Status
Not open for further replies.

simbasaurus

Programmer
May 26, 2007
3
0
0
Hello!

I am trying to make sure that I clean up and free all the resources that I allocate in my code.

In the following code, I am assuming that if new throws bad_alloc, than the value of array1 or array2 is unmodified,
that is, they remain NULL.
In this case, if allocation of array1 succeeds, and allocation of array2 fails, then array1 will be deallocated.
What if new dows modify the value of the int* array2 even if second allocation does fail? Then I will call delete on a value that is undefined!!

I tried this code in Ubuntu using gcc, and when the second allocation fails, then array2 remains NULL, so there is no danger. But is this true for all compilers and systems?
I don't really know what the standard days.


class TwoIntArrays
{
public:
TwoIntArrays();
~TwoIntArrays();

private:
void free_resorces();
int* array1;
int* array2;
};

TwoIntArrays::TwoIntArrays()
{
array1 = NULL;
array2 = NULL;
try{
array1 = new int[200000];
array2 = new int[1000000000];
}
catch(bad_alloc&)
{
free_arrays();
throw;
}
}

~TwoIntArrays()
{
free_arrays();
}

void free_arrays()
{
delete array1;
delete array2;
}


Thank you in advance.
Simbasaurus.

 
How can new return a value if it throws an exception? It can only do one or the other, not both.

Since you are calling free_arrays() in your destructor, why bother calling it in your catch block and then rethrowing the exception? Since free_arrays() isn't setting the pointers to NULL after it deletes them, your program will crash when free_arrays() is called twice (once in the constructor, and again in the destructor).
 
Your code is correct and should work on all standards compliant compilers except for one small error: you should use delete[] instead of delete.
Code:
void free_arrays()
  {
    delete [] array1;
    delete [] array2;
  }

cpjust said:
Since you are calling free_arrays() in your destructor, why bother calling it in your catch block and then rethrowing the exception?
The destructor won't be called if an exception is thrown from the constructor, so you have to call free_array and you don't have to worry about deleting twice in this particular instance. Still, it is a good idea to set the pointers to null in free_array() in case it is called from somewhere else in your code.

I would say that the best solution to this is to use smart pointers. For a dynamic array, the best one is usually vector. In fact, there is rarely if ever a good reason to use a plain dynamic array over a vector. If you used vector, you wouldn't need a destructor, you wouldn't need a try/catch block, you wouldn't need free_array, and you wouldn't need explicit copy construction and copy assignment (which your current code lacks by the way).

Your class would be reduced to this:
Code:
#include <vector>

class TwoIntArrays
{
public:
 TwoIntArrays();

private:
 std::vector<int> array1;
 std::vector<int> array2;
};

TwoIntArrays::TwoIntArrays() : array1(200000), array2(1000000000)
{
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top