| | |
Constructor failure
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
If you initialize all objects in their constructors , those objects which got initialized, destructor will get called as well.
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 ??
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 ??
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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]
In english, this means that, if a constructor throws an exception that;
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.
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]
•
•
•
•
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]
- 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.
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.
•
•
•
•
... when we called constructor , it allocate memory for the object . right .
c++ Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- 8 Puzzle using A_star with Manhattan heuristic (Java)
- encapsulation issue (C++)
- Failing to initialize - what should constructor do? (C++)
- PEAR email (PHP)
- Hi Help me with Multilevel feedback scheduling (Java)
- error C2664 (C++)
- Taking address of constructors?? (C++)
- How to handle exceptions in constructor? (Java)
- overloading Operator << to accept endl (C)
Other Threads in the C++ Forum
- Previous Thread: newbie ... struct database and search and print
- Next Thread: Access voilation
Views: 1314 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






