?
Solved

Deleting A Pointer To A Pointer?

Posted on 2004-03-27
7
Medium Priority
?
221 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 180 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

850 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