| | |
Constructor and Destructor
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
Does the constructor create an instance of a class? or is it a member function of a class that is invoked automatically after the creation of the object is compeleted?
same question about the destructor;
does it deallocate the object itself or does it have the responsibility of making the extra clean up for the memory that was allocated for the use of the object?
i mean; are the creation of an object and constructor, deallocating the object itself and destructor related?
same question about the destructor;
does it deallocate the object itself or does it have the responsibility of making the extra clean up for the memory that was allocated for the use of the object?
i mean; are the creation of an object and constructor, deallocating the object itself and destructor related?
1
#2 Oct 19th, 2009
•
•
•
•
Does the constructor create an instance of a class? or is it a member function of a class that is invoked automatically after the creation of the object is compeleted?
same question about the destructor;
does it deallocate the object itself or does it have the responsibility of making the extra clean up for the memory that was allocated for the use of the object?
i mean; are the creation of an object and constructor, deallocating the object itself and destructor related?
The object's destructor is called at the time the object is destructed.
When you've manually allocated memory inside the object then a default destructor won't automatically free it for you, unless you explicitly write your destructor to deallocate the memory you had previously allocated.
Last edited by tux4life; Oct 19th, 2009 at 4:13 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#3 Oct 19th, 2009
suppose;
regarding to this code, as far as i understand, during the execution of main an instance of class array is instantiated, after this instanstiation process the constructor is executed on the new_array.
and after execution of main, at the end i mean, since now new_array is at the out of its scope it should be destroyed, but this destruction process is before a call to the destructor of the new_array.
am i right or wrong?
C++ Syntax (Toggle Plain Text)
class array{ private: int number_of_elements; int *array_ptr; public: array(int x){ number_of_elements=x; array_ptr = new int[x]; } ~array(){ delete [] array_ptr; } }; void main(void){ array new_array(10); }
regarding to this code, as far as i understand, during the execution of main an instance of class array is instantiated, after this instanstiation process the constructor is executed on the new_array.
and after execution of main, at the end i mean, since now new_array is at the out of its scope it should be destroyed, but this destruction process is before a call to the destructor of the new_array.
am i right or wrong?
•
•
Join Date: Nov 2008
Posts: 394
Reputation:
Solved Threads: 72
0
#4 Oct 19th, 2009
Let use be a little careful here about the sequence of events.
First off, if you examine the assembler/machine code that is 100% exactly what is going on. However, (a) that may mean that your compiler is being "smart" (b) the standard says what the effect should be. So now we will discuss what actually should happen:
So consider your example. First if the code is written as you have written it.
(a) The object is created
(b) then the code in { } below the constructor is called
(c) the delete operator is called
(d) the program exits.
Now let us write a better example:
Now what happens (assuming MyObj is definded elsewhere).
(a) x is initialized with the value 5.
(b) MA is constructed (calling the constructor with two values 4 and 5.
(c) MB is constructed (calling the constructor with no value e.g
(d) Then the assignment operator (=) is called on the previously constructed MB e.g.
(e) the destructor for MB is called
(f) the destructor for MA is called
(g) memory for object A is marked free
e,f,g are the effect of the default destructor begin called.
Does that help / make it clear ??
It is maybe worth playing with the class but add destructor,constructors, copy constructors and assignment operators that printout their name etc.
print out in
First off, if you examine the assembler/machine code that is 100% exactly what is going on. However, (a) that may mean that your compiler is being "smart" (b) the standard says what the effect should be. So now we will discuss what actually should happen:
So consider your example. First if the code is written as you have written it.
(a) The object is created
(b) then the code in { } below the constructor is called
(c) the delete operator is called
(d) the program exits.
Now let us write a better example:
c++ Syntax (Toggle Plain Text)
class Example { private: int x; double d; MyObj MA; MyObj MB; public: Example() : x(5),MA(4,5) { MB=MA; } }; int main() { Example A; }
Now what happens (assuming MyObj is definded elsewhere).
(a) x is initialized with the value 5.
(b) MA is constructed (calling the constructor with two values 4 and 5.
(c) MB is constructed (calling the constructor with no value e.g
MB() (d) Then the assignment operator (=) is called on the previously constructed MB e.g.
MB.operator=(MA); (e) the destructor for MB is called
(f) the destructor for MA is called
(g) memory for object A is marked free
e,f,g are the effect of the default destructor begin called.
Does that help / make it clear ??
It is maybe worth playing with the class but add destructor,constructors, copy constructors and assignment operators that printout their name etc.
print out in
Last edited by StuXYZ; Oct 19th, 2009 at 5:46 pm.
experience is the most expensive way to learn anything
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#5 Oct 19th, 2009
after we call a destructor of an object explicitly, we can still reach the data members of the object. so does not this mean that destructor does not destroy the object as it name implies? also by the aid of this experiment can it be said that the setting the source of memory for the object itself free is related with destructor but they are dinstinct?
•
•
Join Date: Nov 2008
Posts: 394
Reputation:
Solved Threads: 72
0
#6 Oct 19th, 2009
Calling the destructor normally implies that the memory is listed as free, i.e. you can quickly find that the memory is corrupted with something else.
Try valgind to get a detailed picture of deleted and valid memory , as well as gdb/ddd to look at the disassembly.
Try valgind to get a detailed picture of deleted and valid memory , as well as gdb/ddd to look at the disassembly.
experience is the most expensive way to learn anything
![]() |
Similar Threads
- friend, copy constructor, and destructor (C++)
- error: expected constructor, destructor, or type conversion before '<' to (C++)
- Constructor (ASP)
- calling overload constructor in constructor (C++)
- Virtual destructor Problem (C++)
- Class Template Problem - Constructor Issues - Please help (C++)
- constructor problem? (C++)
- (C++) Writing a Copy Constructor?!? (C++)
Other Threads in the C++ Forum
- Previous Thread: Creating a .txt file from a variable
- Next Thread: Basic RLE File compression routine
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






