Jump to content
  • Advertisement

Archived

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

mirirom

deleting pointers to pointers

This topic is 5342 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
Advertisement
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
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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!