deleting mutlidimensional user defined-type arrays (!)

Started by
1 comment, last by mattd 23 years, 9 months ago
um... this dosen't work...
        
struct mapCell {
  short int tile[4];
  bool walkAble;
  int action;
};

map::map(int x, int y) {
  if(x>0 && y>0)
  {
    mapX = x;
    mapY = y;
    grid = new (mapCell *)[x];
    if(grid==0)
    {
      cerr << "Out of memory to alloc new grid X" << endl;
      abort();
    }
    for (unsigned int temp=0;temp<x;temp++)
    {
      grid[temp] = new (mapCell)[y];
      if(grid[temp]==0)
      {
        cerr << "Out of memory to alloc new grid Y" << endl;
        abort();
      }
    }
  }
}

map::~map(void) {
  for(unsigned int temp=0;temp<mapX;temp++)
  {
    for(unsigned int temp2=0;temp2<mapY;temp2++)
      delete grid[temp][temp2];
    delete grid[temp];
  }
  delete grid;
}    
        
(btw: yes there is a class declararation and stuff like mapX and mapY are shared in the class...) first my compiler (djgpp or whatever's behind it) yells at me for " mapping.cpp(49) Error: type `struct mapCell' argument given to `delete', expected...." yada yada so i add &'s to make: for(unsigned int temp2=0;temp2++) delete &grid[temp][temp2]; delete &grid[temp]; you get the idea but then when it try to delete a instance of this class (uh, use the deconstructor ) I get sigsevs, general protection faults on the line: delete &grid[temp][temp2]; // first delete line help me.......... pleaze...... i hope there's a reply tommorrow... My friend brought a new computer with a lifetime warranty. Before it breaks down, it'll electrocute 'em... (to death, people, to death! Now you get it. Now you get it, so you better laugh! HAHAHAHAHAHA) Edited by - mattd on 7/20/00 4:02:49 AM
Advertisement
hey try this in your destructor. This should work ok

    map::~map(void) {     if(grid) //avoid assertion errors   {      for(unsigned int row=0;row<maxRows;row++)        {             //delete the memory in each row         delete[] grid[row];      }      //get rid of the pointer to the rows      delete[] grid;   }}    

"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
I havent deleted any two dimentional arrays for a long time but dont you delete an array like:
delete []somearray;
or
delete [NumberOfMembers]somearray;

so I think you can delete a multi by
delete [#array1members X #array2members]

Im Always Bored
--Bordem
ICQ: 76947930
Im Always Bored--Bordem ICQ: 76947930

This topic is closed to new replies.

Advertisement