?
Solved

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

Posted on 2003-12-11
3
Medium Priority
?
771 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 80 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

800 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