Thread: question about constructors and exceptions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    question about constructors and exceptions

    assume the following conditions:

    a pointer was declared and initialized to NULL before attempting to create an object with new.
    the constructor of the object throws an exception.

    according to standard C++, is the pointer guaranteed to be NULL (no memory was allocated) after throwing the exception? GCC behaves in such a way, but I'm wondering if it's a standard behavior or if it's just implementation specific.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Elkvis
    according to standard C++, is the pointer guaranteed to be NULL (no memory was allocated) after throwing the exception?
    Yes, since the assignment never happens as the exception propagates before it can happen. (Assuming that you are assigning the result to the pointer.)

    Note that the fact that the pointer remains a null pointer has nothing to do with whether memory is allocated or not. A poorly written constructor could allocate memory then throw an exception while failing to have the memory deallocated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by laserlight View Post
    Note that the fact that the pointer remains a null pointer has nothing to do with whether memory is allocated or not. A poorly written constructor could allocate memory then throw an exception while failing to have the memory deallocated.
    I was simply referring to the new that was explicitly called to create the object, and not anything invisible to the programmer inside the constructor, so that answers my question.

    Thank you.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Note that the fact that the pointer remains a null pointer has nothing to do with whether memory is allocated or not. A poorly written constructor could allocate memory then throw an exception while failing to have the memory deallocated.
    Which, IMO, is one good place to use auto_ptr since they will be cleaned up automatically. Of course any boost pointer would be as well but I guess auto_ptr does have a couple of good uses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exceptions and constructors
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2007, 11:20 AM
  2. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  3. Problems with exceptions
    By maneesh in forum C++ Programming
    Replies: 3
    Last Post: 11-23-2005, 05:16 PM
  4. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM
  5. Excaption in Derived Constructors?
    By Davros in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2002, 10:01 AM