Constructor and Destructor

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 5
Reputation: gokhanxyz is an unknown quantity at this point 
Solved Threads: 0
gokhanxyz gokhanxyz is offline Offline
Newbie Poster

Constructor and Destructor

 
0
  #1
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso
 
1
  #2
Oct 19th, 2009
Originally Posted by gokhanxyz View Post
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 constructor is called when (= at the time) an object of a class is created.
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."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: gokhanxyz is an unknown quantity at this point 
Solved Threads: 0
gokhanxyz gokhanxyz is offline Offline
Newbie Poster
 
0
  #3
Oct 19th, 2009
suppose;

  1. class array{
  2.  
  3. private:
  4.  
  5. int number_of_elements;
  6. int *array_ptr;
  7.  
  8. public:
  9.  
  10. array(int x){
  11.  
  12. number_of_elements=x;
  13. array_ptr = new int[x];
  14.  
  15. }
  16.  
  17. ~array(){
  18.  
  19. delete [] array_ptr;
  20.  
  21. }
  22.  
  23. };
  24.  
  25. void main(void){
  26.  
  27. array new_array(10);
  28.  
  29. }

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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
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:
  1. class Example
  2. {
  3. private:
  4. int x;
  5. double d;
  6. MyObj MA;
  7. MyObj MB;
  8. public:
  9. Example() : x(5),MA(4,5)
  10. {
  11. MB=MA;
  12. }
  13. };
  14. int main()
  15. { 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: gokhanxyz is an unknown quantity at this point 
Solved Threads: 0
gokhanxyz gokhanxyz is offline Offline
Newbie Poster
 
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC