[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

delete [] an array of new'd pointers?

Posted on 2004-10-01
5
Medium Priority
?
194 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 375 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

656 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