Public Group

How to delete the complete list

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

Recommended Posts

If DataList2 is the std::list wont the fallowing code delete all of it? Because I am trying and it does not seam to be doing the trick. DataList2.erase (DataList2.begin(), DataList2.end());

Share on other sites
It should. However, use the member function clear to clear the entire list instead of using the function taking an arbitrary iterator range.

Share on other sites
As above, use clear().

Note that if the type contained with the list is a pointer, then delete will not be called on the pointers. The list cleans up what it allocates; you must clean up what you allocate.

Share on other sites
Quote:
 Original post by jpetrieNote that if the type contained with the list is a pointer, then delete will not be called on the pointers. The list cleans up what it allocates; you must clean up what you allocate.

How would I do that?

Share on other sites
Quote:
 Original post by kingpinzsHow would I do that?
Iterate through the container, and delete each element.

	std::list <Type *> Data;	for(std::list <Type *> ::iterator iIndex = Data.begin(), iTail = Data.end(); iIndex != iTail; ++ iIndex)		delete * iIndex;	Data.clear();

Share on other sites
Is there another way to delete it with out using a pointer?

Share on other sites
Quote:
 Original post by kingpinzsIs there another way to delete it with out using a pointer?
I'm not sure I understand what you mean. You only have to manually do what's outlined above if your container stores pointers in the first place.

Share on other sites
I belive that this delete * iIndex; means a pointer to iIndex but I am not sure though. I tryed just delete iIndex it gave me error
type struct std::_List_iterator<int>' argument given to delete', expected pointer

Share on other sites
You need to dereference the iterator to properly delete your data. In other words, "delete * iIndex" is what you should be using.

in this function

{

}

1. 1
2. 2
3. 3
4. 4
frob
12
5. 5

• 16
• 13
• 20
• 12
• 19
• Forum Statistics

• Total Topics
632172
• Total Posts
3004566

×