question about constructors and exceptions

This is a discussion on question about constructors and exceptions within the C++ Programming forums, part of the General Programming Boards category; assume the following conditions: a pointer was declared and initialized to NULL before attempting to create an object with new. ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,539

    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
    22,114
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,539
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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, 12:20 PM
  2. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 08: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, 10:21 AM
  5. Excaption in Derived Constructors?
    By Davros in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2002, 10:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21