T
tanix
Kaz Kylheku wrote:
Stefan Ram wrote:
[snip]
=A0 More elegantly? Actually, for correct and secure C++ code,
=A0 all functions need to be written to be =BBexception safe=AB, but
=A0 only a minority of C++ programmers does so or even is aware
=A0 of it.Why?
The above is false. Exception-safe code is needed to write code
that avoids resource leaks in the face of an exception.For instance:=A0 =A0{
=A0 =A0 =A0 char *p =3D new char[256];
=A0 =A0 =A0 f();
=A0 =A0}hm , why would you do this?
To demonstrate one way in which code fails to be exception safe.
isnt't that
=A0 =A0 =A0{
=A0 =A0 =A0 =A0 vector<char> p(256);
=A0 =A0 =A0 =A0 f();
=A0 =A0 =A0}
is simpler?
This code no longer demonstrates a resource leak in the face of an except= ion,
and so it would not have made a sutitable example to accompany my article= ..
Doh?
I guess what Branimir tried to tell was that you should always release
your ressources in a destructor. This gives you automatically the
basic exception guarantee.
Except that in some cases your destructor is not called.
Plus, James Kanze can tell you more about non-trivial destructors.
:--}
--
Programmer's Goldmine collections:
http://preciseinfo.org
Tens of thousands of code examples and expert discussions on
C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript,
organized by major topics of language, tools, methods, techniques.