Solved

Why is my destructor not being called with delete[] ?

Posted on 2003-12-11
3
768 Views
Last Modified: 2008-03-06
#include <iostream>

using namespace std;

class D
{
public:
      D() {}
      ~D() { cout << "D destructor\n"; }
};

int main()
{
      D** array = new D*[5];

      for (int i = 0; i < 5; i++) {
           array[i] = new D();
      }
      //delete array[1];
      delete[] array;

      return 0;
}

Why am I getting no output when I run the code above ?  I get "D destructor" output when I say, e.g. delete array[0].  I thought that this delete[] would call the destructor for each object in turn !

I have stepped into the code with the debugger and

void __cdecl operator delete(void *p) _THROW0()

is being called; but not my destructor.

Why ?

TIA
0
Comment
Question by:mrwad99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
meow00 earned 20 total points
ID: 9922503
Hello try this
-----------
int main()
{
     D** array = new D*[5];

     for (int i = 0; i < 5; i++) {
          array[i] = new D();
     }
     //delete array[1];
    for(int i=0; i<5; i++){
      delete [] array[i] ;}
    delete[] array;

     return 0;
}
----------------------------
 since it is a two dimension array, you have to do it step by step.

 meow ....
0
 
LVL 19

Author Comment

by:mrwad99
ID: 9922543
>>    for(int i=0; i<5; i++){
      delete [] array[i] ;}
    delete[] array;

second line should be delete array[i].

Yeah cheers for that meow, I was already aware of that, I was just interested as to why the explicit call to delete[] did not work as I thought it would.  Indeed deleting each individual element does result in the destructor being called, as it should.

So I suppose I have answered my own question there.  To delete 2D arrays, the syntax you mentioned should be used.  

GRRRR I AM AN IDIOT ATM AND AM NOT THINKING CORRECTLY.  OF COURSE 2D ARRAYS HAVE TO BE DELETED AS YOU HAVE MENTIONED !

ARGGHHHHHH
0
 
LVL 19

Author Comment

by:mrwad99
ID: 9922571
>> since it is a two dimension array, you have to do it step by step.

Even though I already knew the answer, and indeed answered one of your questions on the same topic, I am going to give you the 20 points, since you have effectively gave me a real hard kick in the butt.  Time for a rest me thinks.

:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question