473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 1580
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sasha | last post by:
Hi, I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes and all... My idea is to have private Guid...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
7
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
18
by: digz | last post by:
Hi , I am trying to write a class that encapsulates a Union within it , I intend the code to be exception safe. can someone please review it and tell me if I have done the right thing , are...
4
by: George2 | last post by:
Hello everyone, Here is Bjarne's exception safe sample, http://www.research.att.com/~bs/3rd_safe.pdf template <class Tclass Safe {
4
by: George2 | last post by:
Hello everyone, The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant). It used...
18
by: Verde | last post by:
I would appreciate your comments on the following two alternatives of a given method. This isn't a real method, as I'm not concerned about the "real work" it could be doing, but would like to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.