Solved

delete [] an array of new'd pointers?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

707 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

16 Experts available now in Live!

Get 1:1 Help Now