Solved

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

Posted on 2003-12-11
3
766 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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
meow00 earned 20 total points
Comment Utility
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
Comment Utility
>>    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
Comment Utility
>> 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now