Re: Exception Safety
On May 24, 10:27 am, jawc...@gmail.com wrote:
{ Some lines are too long. Please fit your text into 70 columns or so;
at most 79 columns. -mod }
I am sort of newbie to C++, reading thru books kind. Read quite a few
but stumbled on Herb Sutter's explanation of exception safety. He says
that the constructor of
template <typename T>
class Stack {
public:
Stack();
~Stack();
private:
T* _v;
size_t _vsize;
size_t _vused;
public:
Stack<T>::Stack() : _v(0), _vsize(10), _vused(0) {
_v = new T[_vsize];
}
is exception safe because
if new throws bad_alloc, no memory is allocated any way (I got this one)
if T() throws,
all the constructed T's are destroyed properly (did not get this) and
delete[] is called *automatically* and frees up the memory (lost
here too)
This is guaranteed by the standard and compilers are required to
implement this. No magic here :-)
I just *did not* get these assertions. I wrote some test code where 5th
construct of T throws exception. Then neither the destructors for the first
4 T's
get called, nor the delete[], nor ~Stack() (if it were to call delete[]).
Can any one help me as to what I am thick on?
Would appreciate if cced too on jawc...@gmail.com
This is probably because you do not have a try/catch around your test
stack object construction and the program terminates due to uncaught
exception _before_ the code to destroy the object gets called.
Try this code (I've adjusted your class a little):
#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <ostream>
template <typename T>
class Stack
{
T* _v;
size_t _vsize;
size_t _vused;
public:
Stack(size_t size) : _v(0), _vsize(size), _vused(0)
{
_v = new T[_vsize];
}
};
class Test
{
public:
Test()
{
static int count = 0;
if (++count == 5)
{
throw std::runtime_error("test error");
}
std::cout << "Test()" << std::endl;
}
~Test()
{
std::cout << "~Test()" << std::endl;
}
};
int
main()
try
{
Stack< Test > stack(10);
return 0;
}
catch (std::exception const& error)
{
std::cerr << "error: " << error.what() << std::endl;
return 1;
}
The code gives me this output:
$ !g
g++ exception-safety.cpp
$ !.
../a
Test()
Test()
Test()
Test()
~Test()
~Test()
~Test()
~Test()
error: test error
$
As you can see, the four Test objects are destroyed properly.
Regards,
Alex Shulgin
PS: Please post a compilable code sample next time :-)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]