• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

delete [] an array of new'd pointers?

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
demingj1
Asked:
demingj1
1 Solution
 
Jaime OlivaresCommented:
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
 
demingj1Author Commented:
// 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
 
efnCommented:
>> 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
 
AxterCommented:
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
 
efnCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now