Solved

Deleting A Pointer To A Pointer?

Posted on 2004-03-27
7
210 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
[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
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 45 total points
ID: 10696207
>>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
ID: 10696227
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
ID: 10696288
>>I have to delete comunItr[0] first? Then I can delete the whole thing?

Yes. That's it.
0
Technology Partners: 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!

 
LVL 23

Expert Comment

by:chensu
ID: 10696293
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
ID: 10699851
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
ID: 10699875
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
ID: 10702620
>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

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 clear a vector as well as how to detect empty vectors in C++.

735 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