1

Let's say I have this simple code:

std::list<Class*> mList
mList.insert(mList.begin(), new Class())

Do I need to worry about manually disallocating the new Class element or is it taken care of by List if I use erase()?

  • Yes, every `new` must be matched with a `delete`. – Code-Apprentice Jun 18 '18 at 15:04
  • Use a smart pointer and then removing element from the container will properly clean up memory and call destructor of `Class` – Slava Jun 18 '18 at 15:04
  • Fore the duplicate in this instance `list` and `vector` are the same and the answer is the same for both. – NathanOliver Jun 18 '18 at 15:05
  • @NathanOliver: while `list` and `vector` are similar for the precise question he asked, as my answer points out there are differences that should be considered. In particular, use of a pointer may be unnecessary for reasons that would not apply to a `vector`. – Jerry Coffin Jun 18 '18 at 15:22

3 Answers3

5

You created it, you need to destroy it.

My advice would be to avoid this if possible (and it usually is). Either put values in the list (usually preferable) or consider using a boost::ptr_list. Or, you might consider something like a std::list<std::unique_ptr<Class>>.

Note that there are substantial differences between std::list and std::vector with respect to the decisions you need to make here. Just for one really obvious example, in a vector, you need to plan for the fact that objects in the collection will be copied/moved as the collection grows. That can rule out storing objects in some cases. With a std::list, however, no copying/moving is ever needed or done. Each node in the list is allocated individually, and once it's allocated it won't be copied or moved. In fact, this is one of the biggest reasons to use a list in the first place.

So, even if your Class defines an identity object that can never be copied or moved, it's still perfectly fine to store it by value in a list (even though you couldn't do the same in a vector).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
4

Yes, in this example, you will have to delete all elements of the list after they are removed from the list, or before list is destroyed.

This is why you should rather use std::unique_ptr - this will ensure it happens automatically.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
-1

Use smart pointers and it will release the memory as appropriate.

#include <memory> 

https://www.google.com/amp/s/www.geeksforgeeks.org/smart-pointers-cpp/amp/

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • This link points to bad advice, it promotes home-brew smart pointers, rather than outlines standard-provided ones. – SergeyA Jun 18 '18 at 15:05
  • Yes it does, but you need to know why it works before you just use something. No understanding means no learning – theCisLife Jun 18 '18 at 15:07
  • I used that link for a reason. If you don't know how smart pointers work you shouldn't use them. Creating leads to understanding which leads to learning. – theCisLife Jun 18 '18 at 15:11
  • 1
    This is the sort of attitude that leads to poorly taught C++. It is far more beneficial to *use* the standard library before delving into implementation details. Otherwise there is no goal in sight, only discomfort at implementing your own square wheel. – StoryTeller - Unslander Monica Jun 18 '18 at 15:13