470,572 Members | 2,421 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,572 developers. It's quick & easy.

safe for exception code?

in the book EC++ chapter 11, with this code

( pb is a member of class )
WIdget& WIdget::operator=(const WIdget& rhs)
{
Bitmap *pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;

return *this;
}

book says this code is safe for exception, but what if exception
occurs in " pb = new Bitmap(*rhs.pb);" this line?

and it's going to run the next line(delete pOrig;). this will result
this object lose its bitmap pointer.

this is right? or wrong?
Dec 30 '07 #1
2 1421
SeniorLee wrote:
in the book EC++ chapter 11, with this code

( pb is a member of class )
WIdget& WIdget::operator=(const WIdget& rhs)
{
Bitmap *pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;

return *this;
}

book says this code is safe for exception, but what if exception
occurs in " pb = new Bitmap(*rhs.pb);" this line?

and it's going to run the next line(delete pOrig;). this will result
this object lose its bitmap pointer.

this is right? or wrong?
Wrong: if

pb = new Bitmap(*rhs.pb);

throws, flow control directly reverts to the corresponding catch-handler.
Consequently, the next line

delete pOrig;

will not be executed in the case of a throw.

A more tricky question is what happens to the variable pb if new succeeds to
allocate memory but the constructor of Bitmap throws. I have a vague
recollection of a discussion on comp.std.c++ regarding this. The standard
was not all that clear to me, but it appears that the intend (if not the
wording) of the standard is that the value of pb remains unchanged if the
rhs throws during evaluation. Thus, the code above should be
exception-safe.
Best

Kai-Uwe Bux
Dec 30 '07 #2
On Dec 30, 12:06 am, SeniorLee <prog3...@gmail.comwrote:
in the book EC++ chapter 11, with this code

( pb is a member of class )

WIdget& WIdget::operator=(const WIdget& rhs)
{
Bitmap *pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;

return *this;

}

book says this code is safe for exception, but what if exception
occurs in " pb = new Bitmap(*rhs.pb);" this line?

and it's going to run the next line(delete pOrig;). this will result
this object lose its bitmap pointer.
Your arguement is mute, if that new allocation throws, everything
after new is skipped, including the return.
Furthermore, the program will unwind each calling scope(s)'s stacks
progressively until the exception is caught ( or in the event thrown
exceptions are left uncaught: terminate() gets called ).
>
this is right? or wrong?
Its right, assuming that member pb was initialized appropriately
beforehand.
Also, that assignment operator should be checking for self-assignment.

WIdget& WIdget::operator=(const WIdget& rhs)
{
if (&rhs == this)
return *this;
// do assignment stuff
return *this;
}
Dec 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Jody Gelowitz | last post: by
reply views Thread by =?Utf-8?B?aGVyYmVydA==?= | last post: by
18 posts views Thread by digz | last post: by
4 posts views Thread by George2 | last post: by
18 posts views Thread by Verde | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.