Solved

Deleting A Pointer To A Pointer?

Posted on 2004-03-27
7
213 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 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