Jump to content
  • Advertisement

Archived

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

papa

When to delete STL containers like list vector etc..

This topic is 5148 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 am wondering whether I need to delete STL containers or theyre destroyed automatically. If not then when should I destroy them? Say I have STL list.
// 1.

std::list<int> ID_Numbers;
ID_Numbers.push_back(89);
// etc... Do I need to delete this list on exit?



// 2. 

std::list<BoundingBox> Boxes;
BoundingBox box1;
Boxes.push_back(box1);
// etc... Do I need to delete this list on exit?


// 3. 

std::list<BoundingBox*> Boxes;
BoundingBox *box1 = new BoundingBox();   // Dynamic allocation

Boxes.push_back(box1);  
// etc... Do I need to delete this list on exit?


// 4. 

std::list<IteratorList> ItrListBoxes;
BoundingBox box1;
std::list<BoundingBox>::iterator itr = list.find(Box);
ItrListBoxes.push_back(itr);  
// etc... Do I need to delete this list on exit?

[edited by - papa on May 29, 2004 11:28:28 AM]

Share this post


Link to post
Share on other sites
Advertisement
1 and 2: No deletion necessary, the list and its contents will be destroyed when the list goes out of scope.

3: You need to go through the list(before it goes out of scope) and call delete on every pointer. The list itself will be destroyed when it goes out of scope.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
Ooops, didn''t see 4. 4 doesn''t need to be deleted either.

The base rule is quite simple: You only call delete on something you called new to create.

Share this post


Link to post
Share on other sites
3) You have to delete the elements of the list, but not the list itself (assuming that you do not call delete on the Box1 variable)


std::list<BoundingBox*>::iterator iter = mylist.begin(), end = mylist.end();

for(;iter != end; ++iter)
delete *iter;



Only delete what you new, and only delete what you new once.

4) list does not have a find member.

Share this post


Link to post
Share on other sites
ok. IC. So to summarize list is destroyed when it goes out of scope and if it contains dynamically allocated structure then the structure must be destroyed before the list is destroyed.
What if the STL list contains a list of iterators (See case 4) or other pointers to some other structure. eg. I have an array of ints
int array[256];

then for eg. I want to store pointers to teh array in STL list structure:

eg.

list<int*> links;
links.push_back(&array[4]);
links.push_back(&array[100]);

Do I need to delete this list. I guess not. Am I right? As I am not using new anywhere. They are simply just pointers to array structure.
So there is no need to call STL clear() or erase() in destructor? I guess non of these functions will delete dynamically allocated memory stored in list either. It must be deleted manually as you mentioned right?



[edited by - papa on May 29, 2004 11:43:05 AM]

Share this post


Link to post
Share on other sites
delete what you new, you did not use operator new there, so you do not need operator delete.

Share this post


Link to post
Share on other sites
Sorry Guys just last question if I could.

std::vector<char*>Names

Names.push_back("name1");
Names.push_back("name2");
Names.push_back("name3");

Will I have to delete each string (char*) manually or it will be destroyed automatically once 'Names' vector will go out of scope of function or on program exit? I know no new so dont delete but just to make sure in this case. Please let me know! thanks!

[edited by - papa on May 29, 2004 3:15:50 PM]

Share this post


Link to post
Share on other sites
Hopefully, this should make it clear. All of these are correct:
{
list foo;
for (int i = 0; i < 10; i++ )
{
int x = i;
foo.push_back( x );
}
...
}

{
list foo;
for (int i = 0; i < 10; i++ )
{
int * px = new int( i );
foo.push_back( px );
}
...
for ( list::iterator it = foo.begin(); it != foo.end(); ++it )
{
int * px = *it;
delete px;
}
}

{
list * pfoo = new list;
for (int i = 0; i < 10; i++ )
{
int x = i;
pfoo->push_back( x );
}
...
delete pfoo;
}

{
list * pfoo = new list;
for (int i = 0; i < 10; i++ )
{
int * px = new int( i );
pfoo->push_back( px );
}
...
for ( list::iterator it = pfoo->begin(); it != pfoo->end(); ++it )
{
int * px = *it;
delete px;
}
delete pfoo;
}
1. New and delete are paired.
2. STL containers are well-designed in that they clean up their mess but they don''t clean up your mess.

Share this post


Link to post
Share on other sites
thank you JohnBolton.

Sorry you must be fed up with me but I dont really know if you declare say:

char *name = "Name";

whether it should be deleted as it appears to me that its a pointer and as far as I now pointers should be deleted. I know there is no 'new' so I guess I should not delete it but thats why I asked about that particular example I posted above.

std::vectorNames
Names.push_back("name1");
Names.push_back("name2");

I thought I should delete it manually as it appreas to me that I am loading pointers. Sorry I am still learning about pointers thats why I am sooo confused.
So whenever I use pointers eg. storing pointers in some array then I dont have to delete those pointers? right? I know that if I create objects using new then those must be obviously deleted. But I was confused about pointers. I thought that pointers must be deleted if they are stored in some data structure. So they are automatically destroyed right?

say:
char *str = "string";
char **pstr = &str;

here the vaaribale str will be destroyed on exit and the pointer pstr too. yes? This is where I was confused.
Forgive me please



[edited by - papa on May 29, 2004 4:46:32 PM]

Share this post


Link to post
Share on other sites
C++ tutorial.
For Beginners Forum.
The data that pointers point to doesn''t have to be deleted unless it was created with new. In the case of ''char* string="string"'', the contents of string--"string"--wasn''t allocated with new, so it doesn''t have to be deleted, simple as that.

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!