Archived

This topic is now archived and is closed to further replies.

deleting pointers to pointers

This topic is 5131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ::..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites