Solved

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

Posted on 2003-12-11
3
769 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
GUI: DIalog Stacking and Popping in MS C++ 4 96
gdb doesn't stop on breakpoint 2 139
Outlook 13 98
find Speed using GPS latitude & longitude 4 156
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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