You are here:

C++/Memory de-allocation

Advertisement


Question
Hi Ralph,
 I'm trying to allocate a 2 dimensional array of pointers dynamically.  I do this in my "Landscape" class's constructor using

Landscape::Landscape(int height, int width){
  
   myCellGrid = new Cell**[height];
   
   for (int i = 0; i < height ; i++) {
       myCellGrid[i] = new Cell*[width];
   }
   
   for (int i = 0; i < height; i++){
       for (int j = 0; j < width; j++) {
         myCellGrid[i][j] = new Cell(this, ,j);
       }
   }

}

However, when testing just this class, I can't get my program to de-allocate without crashing.  My destructor looks like this


Landscape::~Landscape(){
   for (int i = 0; i < height; i++){
       for (int j = 0; j < width; j++) {
         delete [] myCellGrid[i][j];
       }
   }
  
   for (int i = 0; i < height ; i++) {
       delete [] myCellGrid[i];
   }
   
   delete [] myCellGrid;
  
}

It crashes on the first for-loop.  I think it doesnt' like my "delete [] myCellGrid[i][j];", but I don't know what I'm doing wrong.

Answer
There are two forms of new and delete - scalar and array. When deleting the individual cells of your pointer arrays you are using array delete, however they were allocated using scalar new thus:

         myCellGrid[i][j] = new Cell(this, ,j);

(note: I am not sure what the , , in the Cell object constructor argument list is all about!)

i.e. these are the objects allocated with new so they should be deleted with delete, thus:

         delete myCellGrid[i][j];

Those allocated with new [] need to be deleted using delete [], which appear to be OK:

       myCellGrid[i] = new Cell*[width];

deleted using:

       delete [] myCellGrid[i];

and

   myCellGrid = new Cell**[height];

deleted using:

   delete [] myCellGrid;

Please note that I only gave your code a quick look over and spotted this obvious error, they may be more...sorry if there are and I did not spot them.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.