Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:46:44+00:00 2026-05-31T19:46:44+00:00

While going through Effective C++ (by Scott Meyers), I came across the following code

  • 0

While going through Effective C++ (by Scott Meyers), I came across the following code that the author uses to illustrate how exceptions should be handled when copying data members from one object to another.

class Bitmap { ... };

class Widget {
  ...

private:
  Bitmap *pb;                                     // ptr to a heap-allocated object
};

Widget& Widget::operator=(const Widget& rhs)
{
  Bitmap *pOrig = pb;               // remember original pb
  pb = new Bitmap(*rhs.pb);         // make pb point to a copy of *pb
  delete pOrig;                     // delete the original pb
  return *this;
}

In the event that “new Bitmap” throws an exception, pb will remain unchanged. However, by deleting
pOrig, the memory to which pb points has been freed. Isn’t this dangerous? How is it any better than the following code

Widget& Widget::operator=(const Widget& rhs)

{
  if (this == &rhs) return *this;   // identity test: if a self-assignment,
                                    // do nothing
  delete pb;
  pb = new Bitmap(*rhs.pb);
  return *this;
}

Which (he claims) is bad because when “new Bitmap” yields an exception (either because there is insufficient memory for the allocation or because Bitmap’s copy constructor throws one), the Widget will end up holding a pointer to a deleted Bitmap

I checked the book errata, but found no mention of this example. Am I missing something obvious? Also, can someone suggest a better way of handling this exception?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:46:45+00:00Added an answer on May 31, 2026 at 7:46 pm

    The delete pOrig; will be executed if and only if pb = new Bitmap(*rhs.pb); succeeds. If the allocation fails, then no more of this ctor will execute at all — instead, the stack will be unwound, and execution will go from whatever part of the Bitmap constructor threw the exception, directly to the handler for whatever exception was thrown. The only stop along the way will be destroying the variables local to the ctor, but since the only local variable is a pointer, destroying it is pretty much a nop.

    In the case that the Widget object contained any other member variables, any of those that had been completely constructed would also be destroyed as part of the stack unwinding, but since it doesn’t have any (shown) that’s irrelevant here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While going through a Zend tutorial , I came across the following statement: Note
while going through some of the mili source code I came across a struct
While going through the exercises, I came across something that, even after research, I
While going through some C code having inline assembly I came across the .byte
While going through the code of an application, i came across the putpwent() function
While going through the source, I noticed that 'toggle' supposedly uses jQuery._data to store
While going through university and from following the development of SO, I've heard a
While going through a WP7.5 background transfer service policy, one can read the following:
While going through one project, I have seen that the memory data is 8
After going through Marshall code snippet Got the Idea that marshalling is used to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.