tags:

views:

540

answers:

8

How could I free up a linked list that contains dynamically allocated objects? I try to use list<class*> lists, but then I could not use the insert() function to insert object to the list. Does anyone know what is the cause?

A: 

The solution used by the STL is to have the container own the objects it contains. In this scenario each node would be responsible for deallocating it's object when it's destructor is called. You're still responsible for deallocating the copy of the object you pass in.

Edit

If you're attempting to use the stl List and encountering an error, it might be the case that the object you're trying to place inside does not implement a copy constructor. This can happen if you try to stick things other than pointers into the list. If you're comfortable with pointers you might consider storing them in your containers instead of the objects themselves. This will require that you call 'delete' yourself on the pointer when you're done with it. This is typically done by calling 'delete' on the pointer contained in a node when you remove it or by walking the list when you're done with it and calling 'delete' on the contents of each node.

Waylon Flinn
hmm, ok, and do I need to delete the class object that I dynamically allocated using a pointer as well ?
if you use 'new' to allocate your object before you pass it in to the STL list then you need to call 'delete' on that pointer. The container will store a copy (of the pointer) and will not delete the actual object.
Waylon Flinn
+1  A: 

Traverse the list from the beginning to end, and delete one by one.

J.W.
ok, then I need to iterate through my list to delete them.what keyword do I use? is it "delete" or "erase"?do I need to use clear() after I finish deleting the node one by one
Delete, I don't think C++ has "erase" keyword. Read this page : http://www.gamedev.net/community/forums/topic.asp?topic_id=333272
J.W.
+2  A: 

how could I free up linked list that contain dynamically allocated object?

Walk the list, deleting the contained object for each link, or better in some cases, write a link dtor that deletes the contained object.

I try to use list lists, but then I could not use insert()function to insert object to the list. Does anyone know what is the cause?

Not without seeing your code.

tpdi
A: 

Start your project in language/environment, which having an automatic memory management (Garbage collection). Then things like that will be the least which you care about. It will save your time to put your efforts into something else.

Igor Stasenko
Unhelpful response; the (presumed homework) assignment is specifically based on C++.
Dan Breslau
But professor, someone at stackoverflow told me to do something else!!
Tom
And C++ _does_ have resource management.
Arafangion
A: 

You will have to iterate the list and delete the dynamically allocated objects, before clearing the list.

When using containers of newed pointers, please remember to delete the pointers before the container is destroyed.

About problem using insert() : I am not very sure what is the problem you are facing. From the explanation, I assume, you are trying to insert objects of Class into list where as the list contains the pointer to Class. Could you please elaborate the problem with insert?

Pseudo code for deleting the dynamically allocated objects:

version 1: Simple

list<Class*>::const_iterator iter = m_ClassList.begin();
list<Class*>::const_iterator endIter = m_ClassList.end();
for(;iter!= endIter ; ++iter)
{
   delete *iter;
}
ClassList.clear();

version 2: A better version using for_each

struct DeleteClassObject 
{
  //Functor
  template<typename T>
  void operator()(const T* ptr) const
  {
      delete ptr;
   }
}
//loops through list and deletes the dynamically allocated objects using
//functor DeleteClassObject
for_each( m_ClassList.begin(), m_ClassList.end(), DeleteClassObject ());
DeleteClassObject.clear()
aJ
hmm, well I might use the first version. with the insert()problem, I have sorted it out, it is just a foolish mistake of not assigning pointer of the object. however, one quick question, if I populate my list with a dynamically allocated object using pointer, then my list will basically contain pointer that point to the address of my object. so the question is, if I delete my list, am I also considered have deleted my dynamically allocated object as well?
No chris. You will have to delete the dynamically allocated objects ( pointed by pointers) separately using delete and then clear(or destroy) the list. STL containers do not automatically delete the objects inside the containers.
aJ
yeah, I know what you mean. when I iterate through the list and call delete to each node, I assume i have delete the pointer that point to the object as well, am I right?
Yes. when you call "delete <POINTER>"--- it actually deletes the dynamically allocated object pointed by the POINTER.
aJ
ohh, thanks for the reply, by the way, if I got two class, one of it contain a list of the other class ex: list<second*> lists , inside the class private of my major class. and then I create list of the major class list<first*> list, when I call delete *itr on my first class, will it also delete or free the memory of my list<second*> lists that contain dynamically allocated memory ex : second *ptr=new second(). or do I need to write a code to free this memory?
and If do I need to write code on my first class destructor to delete the list<second*>lists which is declare on my first class private section?
You will have to write the code for deleting Second*. If your First class owns the dynamically created objects of Second class then you can delete the Second* in First class destructor.
aJ
my second class only contain list<second*> lists, it does not contain any other member anymore. A pointer to the second class is created in main and pass to the list <second*> lists via insert on my first class. Then if I do need to call delete on my first class destructor, what kind of code do I use?. I try "delete lists" on my destructor but it doesnot work
+3  A: 

std::list<boost::shared_ptr<YourType> > will automatically call YourType::~YourType on each (smart) pointer in the list, when the list is deleted. And if you erase one list element, it will call YourType::~YourType for that one element. You can still call list.insert(new YourType)

Link: www.boost.org

MSalters
or std::list<std::tr1::shared_ptr<YourType> > which will also be in std:: when C++0x comes around.
Motti
A: 

EDIT: THIS IS WRONG, SORRY

If you're using std::list then there's a clear function to delete all objects you put in. std::list test; test.clear(); Oh, and .clear() also invokes the destructors. To use the insert function of such lists you need to specify the position via an iterator. test.insert(test.end(), 5); But there's more than 1 insert function, here you can find more details

EDIT

Could somebody leave a comment when downvoting?

DaClown
clear() calls the elements' destructors' (if such exist) but in the case when they are raw pointers there is no destruction taking place and the memory will be leaked (that is why using boost::shared_ptr is a good way to go, it *does* delete the pointer)
Motti
Oh I see, I didn't know that. I read this http://www.cplusplus.com/reference/stl/list/clear/ but I possibly missed an implication. Thanks.
DaClown
A: 

You could use ptr_list from Boost

http://www.boost.org/doc/libs/1_38_0/libs/ptr_container/doc/ptr_list.html

Tony Edgecombe
Are you sure the destructor deletes the elements? The link you point to doesn't say so.
Motti
Further back in the documentation about boost pointer containers it clearly states they are exception safe. Just to be sure I ran a quick test and the elements are deleted.
Tony Edgecombe