How to delete the complete list

Started by
21 comments, last by kingpinzs 17 years, 2 months ago
he was saying you delete the elements yourself IF they are pointers (and hence you allocated them yourself).

The normal rule is, if you new it, you delete it ... if you don't new it, you don't delete it ... (unless you call code which new's it for you but that's a different sroty).

Simple example:

vector<MyClass> things;vector<MyClass*> things2;//... do stuff adding items and such ...//for things:things.clear();//for things2:for(vector<MyClass*>::iterator pos = things2.begin(), things2.end(); ++pos){  delete *pos;  *pos = 0; // or null of course}things2.clear();


so you see, in the pointer version, you have to destroy your items manually ... because the destruction of a pointer does not automatically call delete for you (for good reason ... many many times you don't want to delete ... only the OWNER of a pointer should delete it).
Advertisement
Quote:Original post by raz0r
You need to dereference the iterator to properly delete your data. In other words, "delete * iIndex" is what you should be using.


No, you shouldn't.

The * operator is used to dereference the pointer, and let the compiler know that you are creating a pointer when you declare it. The * is not actually part of the name.
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
The issue is right now there not pointers. So do I have to make them Pointers?
Quote:Original post by programwizard
Quote:Original post by raz0r
You need to dereference the iterator to properly delete your data. In other words, "delete * iIndex" is what you should be using.


No, you shouldn't.

The * operator is used to dereference the pointer, and let the compiler know that you are creating a pointer when you declare it. The * is not actually part of the name.


Yes, you should

The * operator is being used here to dereference the iterator to get the pointer to delete. That is, to actually dereference the pointer, you'd need to do *(*iIndex). Of course, you'd normally just do *iIndex->, but I felt that the emphasis was necessary.
[TheUnbeliever]
Quote:Original post by kingpinzs
The issue is right now there not pointers. So do I have to make them Pointers?


No. If you're using something like std::list<*myType> then you have to manually delete the elements stored in the list before clearing the list or you'll leak memory. If you're using something more like std::list<myType> then the list will delete the elements itself when you clear it.
[TheUnbeliever]
Quote:Original post by kingpinzs
The issue is right now there not pointers. So do I have to make them Pointers?


Does the list contain pointers? If it does, then you have to delete them before you clear() the list. If the list just contains normal objects (and it looks like it does from the code you posted), just call list.clear()

EDIT:

Quote:Original post by TheUnbeliever
The * operator is being used here to dereference the iterator to get the pointer to delete.


Ah, so the iterator is a pointer to a pointer? Thanks for clearing that up.
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
no, it is not a pointer, but it is like a pointer. If you have an iterator into a list you can dereference it to get to the data stored in one of the nodes. You can also increment the pointer. Then when you dereference it, it will give you the data in the next node. A pointer to a node or to the data in a node wouldn't work that way.
How can I change this

void Add(POINT pos,RECT Src)
{

paddle.Pos=pos;

paddle.Src=Src;

Sprites.push_back(paddle);

}

to just remove the paddle.Pos from the list<SPRITE> Sprites;

the struct is

typedef struct {    int x,y;    int width,height;    int movex,movey;    int curframe,lastframe;    int animdelay,animcount;    int scalex, scaley;    int rotation, rotaterate;    POINT Pos;    RECT Src;} SPRITE;

I am guessing this is due to a translation error of 'remove'. Is this what you want to do?
POINT thePointYouWant = Sprites[0].Pos;

If you are trying to remove a single member element from the struct then that isn't possible. You can reset it to a default value but not remove it from memory.

Steven Yau
[Blog] [Portfolio]

AS far as I know I just want to remove a member from the stl::list and the member type is the struct.

This topic is closed to new replies.

Advertisement