Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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