×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

behaviour of "new" on bad_alloc

behaviour of "new" on bad_alloc

behaviour of "new" on bad_alloc

(OP)
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.

RE: behaviour of "new" on bad_alloc

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).

RE: behaviour of "new" on bad_alloc

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;
  }

Quote (cpjust):

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)
{
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close