Skip to main content
GameDev.net gamedev.net
🔒 Locked

Deallocatnig a 2D array

Started by DekuTree64 Oct 11, 2000 at 2:50 PM 3 replies 700+ views
Original Post
DekuTree64
DekuTree64
Well, I have a 2D array of tiles for my map, but when I try to delete[] them, sometimes it causes an error, and sometimes it just exits, but leaves the VC++ window deselected (err, whatever you call it when the title bar''s grey^^). Here''s the code I use to create it, then the code to delete it
    
   grid = new tile*[x];
   if(*grid)
   {
      for(int i = 0; i < x; i++)
         grid<i> = new tile[y];
   }

   if(grid)
   {
      for(int i = 0; i < x; i++)
         delete [] grid[i];
      delete [] grid;
   }
    
Anybody know how to make it work? I need to get to work on the actual tile engine, but I can''t until the destructors working or I''ll end up having to reset every once in a while to clear out my RAM...
-Deku-chan DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^) "I'm dropping like flies!" - me, playing Super Smash Bros. (and losing) "What fun!" - me, playing Super Smash Bros. (and beating the crap out of somebody)
Trancelucid
Trancelucid
Here's a complete re-write of how I'd do it

        
tile **grid = NULL;

grid = new tile*[x];
if( grid )
{
for( int i = 0; i < x; i++)
{
grid<i> = new tile[y];
if( NULL == grid )
{
// error -- lack of memory

}
}
}

if( grid )
{
for( int i = 0; i < x; i++ )
{
if( grid<i> )
{
delete []grid[i];
grid[i] = NULL;
}
}
}
delete []grid;
grid = NULL;


-The first if() must not have * before grid (you want to know if the pointer is valid, not if the value pointed by grid is non-zero).

-The grid<i> kinda confuses me here , never seen that before.. is it a typo on your part, or is it me being ignorant?

-Always initialise pointers to NULL, and check if pointers are valid before using them.

If this still doesn't work, comment everything and run again to isolate the bug. I helped a friend of mine on his homework this weekend, and he asked me the same exact question (and he had implemented it right too). After a few hours of debugging, we found out it was a call to the fstream class that bugged...

HTH,
.(Trancelucid).

Edited by - Trancelucid on October 11, 2000 11:19:15 PM
DekuTree64
DekuTree64
It seems to be working better than before now, but still not right. I did some checking, and the error is in delete [] grid; right at the end, but I can't figure out why.
Oh, and the <i> thing is supposed to be [ i ], I think I just got mixed up while changing the <s in the for loops.
Anyway, thanks, and I'll keep messing with it until I knock myself out smashing my head into the corner of the computer^^



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

"I'm dropping like flies!" - me, playing Super Smash Bros. (and losing)
"What fun!" - me, playing Super Smash Bros. (and beating the crap out of somebody)

Edited by - DekuTree64 on October 12, 2000 2:21:25 AM
Trancelucid
Trancelucid
The most common problem that I''ve seen about dynamic arrays so far is to write outside of it for some reason. VC++ would say it''s the delete[] part that bugs, but in fact it''s another part in your program that overwrites some data (most probably data that delete[] needs).

Try commenting all the code and run, if it works uncomment line by line to see where exactly it bugs.

HTH,
.(Trancelucid).
DekuTree64
DekuTree64
Yay, it worked^^ The problem was I have a map class, which has a dynamic array of layers, which each have a dynamic 2D array of tiles, and I was initializing them like this
    	mapLayer = new layer[l];
if(mapLayer)
{
for(int i = 0; i < l; i++)
{
mapLayer<i> = layer(lpTileSurf, xSize, ySize);
}
}

so when I''d go set the tile surface and size variables with the constructor, it was allocating a new array of tiles, which of course isn''t a good thing.
Thanks for the help, now I can get into the even harder stuff and possibly kill myself^_^
Either that or everything will go smoothly and I''ll be nice and happy.



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

"I'm dropping like flies!" - me, playing Super Smash Bros. (and losing)
"What fun!" - me, playing Super Smash Bros. (and beating the crap out of somebody)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.