Solved

Deleting A Pointer To A Pointer?

Posted on 2004-03-27
7
207 Views
Last Modified: 2010-04-01
If I have ListItr ** columnItr = new ListItr *[k]; can I just use delete [] columnItr; or do I need to delete it some other way?
0
Comment
Question by:JohnnyIshot2004370153
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 45 total points
Comment Utility
>>can I just use delete [] columnItr;

Yes. But, make sure that the contents of the pointer array get deleted, too, if they were allocated dynamically.
0
 

Author Comment

by:JohnnyIshot2004370153
Comment Utility
columnItr[0] = new ListItr(temp);

I have to delete comunItr[0] first? Then I can delete the whole thing?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>I have to delete comunItr[0] first? Then I can delete the whole thing?

Yes. That's it.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 23

Expert Comment

by:chensu
Comment Utility
The basic rule is that one new (new []) must have a corresponding delete (delete []). So,

ListItr ** columnItr = new ListItr *[k];
columnItr[0] = new ListItr(temp);
...

...
delete columnItr[0];
delete []columnItr;
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Maybe you should have a new class that handles all of that:

class ColumnItr
{
private:
     int          m_cols;
     ListItr** m_pListItr;
public:
    ColumnItr(int cols, int tempArr[]) : m_cols[cols]
    {  m_ppListItr = new ListItr*[m_cols];
      for (int i = 0; i < m_cols; i++)
         m_ppListItr[i] = new ListItr(tempArr[i]);
    }
    ~ColumnItr()
    {
        for (int i = 0; i < m_cols; i++)
           delete m_ppListItr[i];
        delete [] m_ppListItr;
    }
    ListItr* operator[] (int col) {  return (col >= 0 && col < m_col)? m_pListItr[col] : NULL; }

};

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Sorry, there have been some typos:

That one compiles:

   
class ColumnItr
{
private:
     int          m_cols;
     ListItr** m_ppListItr;
public:
    ColumnItr(int cols, int tempArr[]) : m_cols(cols)
    {  m_ppListItr = new ListItr*[m_cols];
      for (int i = 0; i < m_cols; i++)
         m_ppListItr[i] = new ListItr(tempArr[i]);
    }
    ~ColumnItr()
    {
        for (int i = 0; i < m_cols; i++)
           delete m_ppListItr[i];
        delete [] m_ppListItr;
    }
    ListItr* operator[] (int col) {  return (col >= 0 && col < m_cols)? m_ppListItr[col] : NULL; }

};

Regards, Alex
0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
>If I have ListItr ** columnItr = new ListItr *[k]; can I just use delete [] columnItr; or do I need to delete it some other >way?

For a proper cleanup:

for(int i=0;i<k;i++)
  delete ListItr[i];  //only if every ListItr[i] has been "new"-ed
delete[] ListItr;
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

744 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

14 Experts available now in Live!

Get 1:1 Help Now