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

deleting pointers to pointers

Started by mirirom Nov 30, 2003 at 12:32 PM 2 replies 350+ views
Original Post
mirirom
mirirom
hi, i just want to double check my logic on this. basically i have an object that contains a pointer to an array of pointers. when the destructor of this object is called, i (obviously) want to be sure that i first delete each elements heap memory prior to deleting the pointers on the heap. for example...
class Item{//...};


class foo{
   Item** pItemList;
   Item()
   {//...build an itemList}

   ~Item()
   {
      for(int x=0; x < itemCount; x++)
      {
         delete itemList[x];  //delete heap mem at element x    

      }
      delete[] itemList;
   }   
};
am i on track with this? pointers to pointers to... always confuse me a bit. ..:: mirirom ::..
..:: mirirom ::..
Koen
Koen
You didn''t say how you did allocate the memory, but your code is probably correct (considering "Item** pItemList;"). Just keep in mind that for every new there should be a delete.
sphinx23
sphinx23
this should work, but: why don''t you use std::vector ?
its much cleaner to use and the memory work has allready been done for you
mirirom
mirirom
thx guys, much appreciated. and yep, memory within the array is being allocated dynamically, e.g.

void appendItem()
{
extendArray(itemList); //extending logic call

Item* pTemp = new Item(); //new Item on heap

itemList[itemCount++] = pTemp; //new pointer at current index

}


as for std::vector, i wish i could use it, but i''m testing myself for a final exam. thanks again!
..:: mirirom ::..

Topic Locked

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

Sign in to reply to this topic.