Solved

Deleting A Pointer To A Pointer?

Posted on 2004-03-27
7
212 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
getting a fire fox page (view page source ) in c++ 6 47
Need some help with Microsoft Visual Studio C++ 2003 5 85
No module found pypyodbc, 3 53
Finding Divisors 5 58
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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 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.

739 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