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?

1
  • 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.) Commented Jan 14, 2012 at 21:44

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
@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.
Sure, but a lot of people aren't using compilers that use the latest C++ standards yet.
@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.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.