Solved

delete [] an array of new'd pointers?

Posted on 2004-10-01
5
191 Views
Last Modified: 2012-05-05
I have a fairly dynamic html table building class set up.  When you declare a table it allocates a 2 dimensional array of pointers as follows.

//***** INSTANTIATION *****
ReportTable rt(5, /*num cols*/
                      4, /*num rows*/);  // more arguements are available but they are N/A

//***** CLASS DEFINITION *****
// private class member
ReportTableCell*** _td;

//***** CONSTRUCTOR *****
// chunk to allocate all the memory for the table cells
_td = new ReportTableCell**[_cols];
for (unsigned i=0; i<_cols; i++)
{ _td[i] = new ReportTableCell*[_rows]; }
for (unsigned i=0; i<_cols; i++) for (unsigned ii=0; ii<_rows; ii++)
{ _td[i][ii] = new ReportTableCell(); }

//***** DESTRUCTOR *****
// deallocate the memory we have allocated
for (unsigned i=0; i<_cols; i++) for (unsigned ii=0; ii<_rows; ii++)
{ delete _td[i][ii]; }
for (unsigned i=0; i<_cols; i++)
{ delete [] _td[i]; }
delete [] _td;

//***** QUESTION *****
As you can see here in the destructor I go through and "delete []" the pointers i have new'd in the constructor.  Is it necessary to delete these pointers or do they get released when they go out of scope.  I have learned to "delete []" or "delete" everything I "new" but pointers are a bit different, however I am allocating an array of those... which is correct, I know both seem to compile and run OK.  Thank you.

mith





0
Comment
Question by:demingj1
[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
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12201148
Surely YOU HAVE to delete all data you have created with new, but, talking about pointers, you have to delete all pointed data FIRST.

0
 
LVL 2

Author Comment

by:demingj1
ID: 12201240
// this would delete all the pointed data first
for (unsigned i=0; i<_cols; i++) for (unsigned ii=0; ii<_rows; ii++)
{ delete _td[i][ii]; }

// note I use "delete" here because I am looping through the array of each pointer and deleting each "ReportTableCell" specifically

// then I erase the pointes.
for (unsigned i=0; i<_cols; i++)
{ delete [] _td[i]; }
delete [] _td;

or are you saying I should do it in reverse?
anything else wrong here?
I am asking because I am getting some random bad_alloc and memory-related crashes on release builds, it very well could be elsewhere, but I need to be sure the base of the class is set up right first.
0
 
LVL 15

Accepted Solution

by:
efn earned 125 total points
ID: 12205803
>> Is it necessary to delete these pointers or do they get released when they go out of scope.

It is necessary to delete them, just like anything else that is dynamically allocated.

Your code looks OK to me.  Deleting in the reverse order of creation, as it does, is the correct approach.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12205914
You're using variables that begin with an underscore.

The above code is not portable according to the C++ standard.

The C++ standard reserves names that begin with an underscore for the implementation.

***************************************************************************
17.4.3.1.2 Global names
1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase
letter (2.11) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
***************************************************************************

Also, I recommend avoiding the use of new as much as possible.
There are usually safer alternatives, like using std::vector's for arrays.
When you do use new, I recommend using a smart pointer.
0
 
LVL 15

Expert Comment

by:efn
ID: 12206097
The member variable name "_td" does not contain a double underscore nor begin with an underscore followed by an uppercase letter, and it is not in the global namespace.  Therefore, it is not a name that is reserved to the implementation.

However, I agree as a matter of style that it is better to avoid such names.  And this page warns that some compilers may not conform to the standard in this matter, so as a practical matter, it may be safer to avoid all names with leading underscores:

http://oakroadsystems.com/tech/cppredef.htm#Groups
0

Featured Post

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!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

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