2

I did practicals on Destructor but when compile this program I don't know why the output not come as I thought.

#include <iostream>
using namespace std;
class aaa
{
   private:
   static int x;
   int code;
   public:
   /*after constructor executes 3 times the value of "code" becomes 103*/
  aaa()   
  {
    code=x;
    cout<<"Default Constructor"<<endl;
    x++;
  } 
  ~aaa() 
  {
    cout<<"Destructor of "<<code<<endl;
  } 
};
int aaa::x=101;
int main() 
{
   aaa *p;
   p=new aaa[3];
   delete []p;
   return 0;
 } 

Output is:

Default Constructor
Default Constructor
Default Constructor
Destructor of 103
Destructor of 102 
Destructor of 101

while I thought it was going to be this:

101
102
103
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Aman Warudkar
  • 125
  • 1
  • 1
  • 12

3 Answers3

3

Destruction happens in the opposite order of construction, that is why you see the destructor for 103 called first.

i.e. When you allocate the array, new[] constructs the objects in one direction and then when you call delete[] the objects are destroyed from the end of the array.

See @StoryTeller's answer for a quote from the C++ standard regarding this behavior.

Curious
  • 20,870
  • 8
  • 61
  • 146
  • and what happens if i write delete p instead of delete []p, so ho many times Destructor is called – Aman Warudkar Jun 28 '17 at 10:13
  • @AmanWarudkar see https://stackoverflow.com/questions/6953457/delete-and-delete-are-the-same-when-deleting-arrays you should always delete arrays allocated with `new[]` with `delete[]` – Curious Jun 28 '17 at 10:13
2

Destructors are invoked in reverse order to object initialization, and that is true for arrays destroyed by delete[] as well:

[expr.delete/6]

If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the completion of their constructor; see [class.base.init]).

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
1

I don't know why the output not come as I thought.

Because the objets are destructed in reverse order of construction: First constructed, last destructed.

The destructors are called in the reverse order of the constructors' calls, which explains the behavior you see.

So when you dynamically allocate memory with new[] for your array, the constructors are called in the natural order (the one you expect), but when delete[] is called to free that memory, every element of the array gets destructed in the reverse order.

Read more in Why destructors are not called in reverse order for array of objects?


What happens if i write delete p instead of delete []p, so ho many times Destructor is called?

C++ requires that you delete arrays with delete[] and delete non-arrays with delete. So I can't answer that. Read more in Delete and delete [] are the same when deleting arrays?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • and what happens if i write delete p instead of delete []p, so ho many times Destructor is called – Aman Warudkar Jun 28 '17 at 10:08
  • @AmanWarudkar https://stackoverflow.com/questions/6953457/delete-and-delete-are-the-same-when-deleting-arrays – Curious Jun 28 '17 at 10:10
  • 1
    @AmanWarudkar updated my answer, hope this helps! Do not forget to *accept* an answer. Curious thanks for the link and nice answer BTW. – gsamaras Jun 28 '17 at 10:14