Hi, I want to know what exactly happens when constructor fails. Do we have mechanism which will provide us information regarding failure of constructor other than exception.
What happens with the memory already allocated to the object when in some circumstances construction of object fails.
Why should a constructor fail?
When you declare a constructor, all the instances of the class will have to use it, or variations of it (if you declare some).
Last edited by unbeatable0; Dec 16th, 2008 at 8:40 am.
What kind of answer was that.
You guys are talking about the state of an object which is already created and answering in that circumstance.
But my context is when object is in creation phase, it has not yet created and constructor fails. There may be various reason like shortage of memory or so.
So why constructor fails is entirely different issue. I am wondering what happens when constructor get fails and how compiler deals with this issue.
Another relative question: let we has a class which is having 20 members. when we called constructor , it allocate memory for the object . right . What hapens when memory cant be allocated, and what happens when 10 members hass been initialized and remaining 10 members still waiting for initialization and we run out of memory.
So wht'll happen. The object will in which state. Will it get destroyed and memory will be released OR we will have memeory leaks ??
The only way in which a constructor can report an error is by throwing an exception.
The C++ standard has this to say (in Section 15.2 "Constructors and destructors" which is within Section 15 "Exception Handling"). [Note: I have not preserved font changes or emphasis in the quote]
Quote originally posted by C++ standard ...
1 As control passes from a throw-expression to a handler, destructors are invoked for all automatic objects constructed since the try block was entered. The automatic objects are destroyed in the reverse order of the completion of their construction.
2 An object that is partially constructed or partially destroyed will have destructors executed for all of its fully constructed subobjects, that is, for subobjects for which the constructor has completed execution and the destructor has not yet begun execution. Should a constructor for an element of an automatic array throw an exception, only the constructed elements of that array will be destroyed. If the object or array was allocated in a new-expression and the new-expression does not contain a new-placement, the deallocation function (3.7.3.2, 12.5) is called to free the storage occupied by the object; the deallocation function is chosen as specified in 5.3.4. If the object or array was allocated in a new- expression and the new-expression contains a new-placement, the storage occupied by the object is deallocated only if an appropriate placement operator delete is found, as specified in 5.3.4.
3 The process of calling destructors for automatic objects constructed on the path from a try block to a throw- expression is called “stack unwinding.” [Note: If a destructor called during stack unwinding exits with an exception, terminate is called (15.5.1). So destructors should generally catch exceptions and not let them propagate out of the destructor. —end note]
In english, this means that, if a constructor throws an exception that;
Any members or base classes of the class that have been fully constructed will have their destructors invoked (in reverse order of their construction)
Destructors will be invoked for any objects that are constructed locally within the body of that constructor (ie auto variables that are local to the body of that constructor).
If the constructor that throws the exception has successfully done any non-trivial operations (eg assigning the result of operator new[] to a pointer) then those non-trivial operations need to be undone before the exception is thrown, as they will not be undone.
This is one reason it is a good idea to use initialiser lists: each successfully initialised member is constructed, so if one of the initialisers fail (with an exception) the ones that are successfully completed will be undone.
Note that usage of placement operator new changes things a bit. But, if you're using a placement new/delete you would already understand the principles of what happens if an exception is thrown during object construction.
Last edited by grumpier; Dec 17th, 2008 at 3:34 am.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.