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.

Recommended Answers

All 6 Replies

If you initialize all objects in their constructors , those objects which got initialized, destructor will get called as well.

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).

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]

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;

  1. Any members or base classes of the class that have been fully constructed will have their destructors invoked (in reverse order of their construction)
  2. 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).
  3. 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.

commented: good +1

... when we called constructor , it allocate memory for the object . right .

A constructor does not allocate memory for the object. It does not know how to do that. Think about:

class C { public: C(); C operator+(const C&); }
...
C c; // static storage
void f() {
     C c; // automatic storage
     ...
     C* pc = new C(); // dynamic storage
     ... *pc + C ... // unspecified temporary storage

It's the same C::C() called!

I wanted to convey "When we instantiate the class it allocates memory for the object."

Any ways "grumpier" has cleared all my doubts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.