Re: Exception Safety
* jawckey@gmail.com:
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.
In which book, and where in that book?
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)
The expression "new T[_vsize]" (I can't believe Herb is using a leading
underscore, it would be a bad habit to teach, and I can't believe he
missed the final semicolon, but then, perhaps he did) allocates and
initializes an array of T's.
After allocating memory for the array, the code generated by the
compiler calls the T default constructor on each element of the array.
If one of the T constructor calls throws an exception that has a
handler, the destructors of the already constructed T objects (in this
array) are called in the opposite order, "destroying" them in the sense
of letting them reverse the actions of the constructor calls.
Then, the earlier allocated array memory is deallocated.
Finally, the exception is rethrown.
and
delete[] is called *automatically* and frees up the memory (lost
here too)
Yes, this one is very confusing.
Instead of a reasonable name like "deallocate", C++ calls the (array)
deallocation function "operator delete[]", which is /not/ the same as a
delete[] expression
delete[] _v;
The delete[] expression invokes, as its last action, operator delete[]
(which could be one defined by class T). With more reasonable, less
confusing names, you could say that the delete[] expression, after
invoking the destructors, invokes deallocate().
And with more reasonable, less confusing names, you could say that, in
the case above, deallocate() is called automatically and frees up the
memory.
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[]).
Post the code.
Can any one help me as to what I am thick on?
Would appreciate if cced too on jawckey@gmail.com
Generally, if you ask here, then you read and discuss it further here.
Without that harsh rule Usenet could easily become a place where only
questions were posted. And who'd answer them, then?
By the way, note that although the class above is in a sense exception
safe, it's not safe in other ways. The lack of a user-defined
destructor means it will leak memory, and if a destructor was introduced
then also a copy constructor and a copy assignment operator should be
introduced. But I guess Herb discusses that.
Hth.,
- Alf, 24.05.2007 09:48
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]