| Zapeth (79) | |||
|
Hello, I'm currently trying to figure out how to store the copy of an object in a std::list so the data of the list entry remains untouched when the object I was getting the data from is getting destroyed. Now I know I can simply add an entry in a std::list with push_front()/push_back() but I can't seem to find any clear statement if the object that gets added to the list is a copy or just a pointer of/to the original object.I already tried to test some stuff with sample code but I'm not sure what to make out of it. Maybe someone can explain to me what is happening in the following sample code:
Since the data in object_list was not "deleted" I dare to ask: Is this the way to do what I wanted to do? Or is there some other, "proper" way to do this? | |||
|
Last edited on
|
|||
| EssGeEich (1007) | |
| It is a copy. The copy constructor is called. To do it how you want to, you should not delete the pointer and keep reference of how many time it is used (Reference counting). | |
|
Last edited on
|
|
| Zapeth (79) | |
|
Well in practice I don't delete the pointer manually, I just want to be sure that the data stays untouched in the list after it was being put in it. Would it be best to use a smart pointer for the reference counting? Also does the above code produce any memory leaks in that matter or was the data being copied in a clean way into the list until it gets properly deleted out of the list with erase()/clean()/pop_front()/pop_back?Or to put it in another way - is the above code also usable for me or should I avoid coding like this? :P | |
|
Last edited on
|
|
| EssGeEich (1007) | |||||
|
It's not so simple to use a smart pointer for reference counting. and no, it doesn't get a memory leak, but the pointer in pointer_list is nomore valid. Your idea should be like using only one of the twos:
Or:
| |||||
|
|
|||||
| Zapeth (79) | |||
In my application I will of course use only one of them. Anyway, thanks for the quick help ;) | |||
|
|
|||
| EssGeEich (1007) | |
|
Ah, I forgot a thing: In case you use the first example i showed you up ( std::list<Object*> ptrlist; ) When you delete the std::list you should delete all the internal pointers. Or you can use a smart pointer but you will not be using reference counting ( std::list< std::auto_ptr< Object > > ptrlist; )
| |
|
|
|
| Zapeth (79) | ||
|
You mean I should manually call delete for each entry in the list? But doesn't clear() do that for me?http://www.cplusplus.com/reference/list/list/clear/
| ||
|
Last edited on
|
||
| EssGeEich (1007) | |||
It calls the destructor, but if it's a pointer you manually allocated, you must manually destroy it too, unless you use std::auto_ptr.
| |||
|
Last edited on
|
|||
| Zapeth (79) | |||
So doing it like this would be fine (no memory leaks)?
edit: sadly I can't call the list entries by index but I can iterate through it so that should be fine too | |||
|
Last edited on
|
|||
| Peter87 (3912) | |
| If you want to access the elements with index you should consider using std::vector instead of std::list. | |
|
|
|
| Zapeth (79) | |
|
No I don't need to access the indices, I just said that because EssGeEich used that in his commented code so I wanted to point that out. I'm fine with using std::list ;) | |
|
Last edited on
|
|
| Disch (8615) | |
|
EssGeEich is correct in that you should use a smart pointer so you do not have to manually delete these objects. However you should not use auto_ptr because it's deprecated (and it doesn't even work when used in standard containers). Replace it with unique_ptr or shared_ptr. | |
|
Last edited on
|
|
| EssGeEich (1007) | |||
It's me who isn't used to std::, but yeah, for each element that gets removed, you need to call delete on it, so that example works. Not doing this won't result in an error, but in a memory leak. | |||
|
|
|||
| Zapeth (79) | |
| Okay, thanks again for explaining everything to me :) | |
|
|
|