1

I am having some difficulty understanding how containers are implemented in C++. Specifically, how can I deal with data allocated on the stack vs data allocated on the heap. For instance:

vector<int> VectorA;
VectorA.push_back (1);
VectorA.push_back (2);
VectorA.push_back (3);

vector<int*> VectorB;
VectorB.push_back (new int (1));
VectorB.push_back (new int (2));
VectorB.push_back (new int (3));

How does one deal with making sure the integers in VectorB are deleted properly. I remember reading somewhere that std::vector only calls the destructor and doesn't actually delete anything. Also, if I wanted to implement my own LinkedList class, how would I deal with this particular problem?

Pubby
  • 51,882
  • 13
  • 139
  • 180
Dave
  • 7,283
  • 12
  • 55
  • 101
  • 1
    In the latter case, if you don't use smart pointers, then anywhere you remove items from the vector, you have to remember to delete the object referenced by the pointer. You must also do this before the containing class is destroyed (delete any referenced objects.) – Joe Jan 14 '12 at 21:44

3 Answers3

4

The ideal way to deal with the problem is by using Smart Pointers as elements of your container instead of raw pointers.
Otherwise you have to manually do the memory management yourself.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    Make sure you use a smart pointer that supports copying (since containers make copies internally), like `boost::shared_ptr`. Do not use `std::auto_ptr`, for instance, as it is not compatible with container copy semantics. – Remy Lebeau Jan 15 '12 at 05:49
  • @RemyLebeau-TeamB std::auto_ptr is anyways deprecated now. I am not sure Als is recommending it. std::unique_ptr or std::shared_ptr can be used. – Jagannath Jan 15 '12 at 10:47
  • Sure, but a lot of people aren't using compilers that use the latest C++ standards yet. – Remy Lebeau Jan 15 '12 at 17:02
  • @RemyLebeau-TeamB: My answer attempts to educate an user unaware of an entity called `Smart Pointers`,It purposefully doesn't delve in to the depths because I do not want to feed answers.I would rather have the user use a `std::auto_ptr` face problems, investigate and learn from those problems than just use a `std::shared_ptr` because We told him so.I would rather point someone to the right direction instead of giving them a lift to the destination.For, I believe *Give a man a fish and he will feed for a day,teach him to fish and he will feed forever on his own.* – Alok Save Jan 15 '12 at 17:10
2

The objects stored in the vector are stored on the heap anyway (in space allocated by the vector).

There is very little advantage in allocating the objects separately (option B), unless you intend to manage them somehow outside of the vector. In that case the vector can just destroy the pointers it stores, and trust the real owner to destroy the objects themselves.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

The short answer is that most don't deal with such things at all. The standard containers (e.g., std::vector) don't store the original object at all -- they store a copy of the object. They manage the storage for their own copy, and it's up to you to manage the storage for the original.

In the case of storing pointers, you're basically taking responsibility for managing the storage pointed to by the pointer. The container is still making (and managing the storage for) a copy, but it's just a copy of the pointer, not the object to which it refers. That's up to you to deal with.

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