Again, for exception safety, take a look at
http://www.stroustrup.com/except.pdf
it uses a vector implementation as example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
template <typename T>
void MyVector<T>::reallocate()
{
try
{
T* temp = new T[capacity_ * 2]; //(a)
for(unsigned int i = 0; i < size_; i++)
{
*(temp + i) = *(data + i); //(b)
}
delete[] dat
data = temp;
capacity_ *= 2;
}
catch(std::bad_alloc& e)
{
std::cout << e.what() << "\n";
}
std::cout << "Reallocation successful.\n";
}
|
Lines (a) and (b) may thrown an exception.
If (b) also throws `bad_alloc' you may not be able to tell which one are you catching.
For (b) you need to deallocate the buffer, and destroy all the objects that were `created'.
Edit: as you are not using placement new, all the objects are already constructed.
I suppose that you could simply
delete[] temp which will release everything properly. (you'll need to increase `temp' scope)
By the way, ¿why the `*(temp+i)' obfuscation?