Solved

delete [] an array of new'd pointers?

Posted on 2004-10-01
5
188 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now