Re: Should I use pointer inside container?
On 2007-09-09 13:34, terminator wrote:
On Sep 8, 5:27 pm, Erik Wikstr?m <Erik-wikst...@telia.com> wrote:
On 2007-09-08 16:04, Goran wrote:
Hi @ all!
Again one small question due to my shakiness of what to use...
What is better / smarter?
private:
vector<MyClass_t> * itsVector;
OR...
private:
vector<MyClass_t * > * itsVector;
None of the above, use either
std::vector<MyClass> itsVector;
or
std::vector<MyClass*> itsVector;
Which one you should use depends on how you will use the elements, if
you have not special needs the first one is generally preferable since
it removes the need to new and delete the elements manually.
There might be a few legitimate situations where you might need a
pointer to a vector, but I would say that it is most often a sign of bad
design.
How works such a stl container? Does it use heap or stack?
The elements contained in the vector are stored on the heap, notice
thought that for some containers, std::vector among them, the elements
might be re-located when you perform some operations on the containers,
so taking the address of an object can be hazardous.
In fact, I would say that when you find yourself using a pointer ask
yourself if you really need it. In most cases allocating on the stack or
using a reference is just as good.
--
Erik Wikstr?m
I tend to consider containers as very huge objects(due to the
unpredictable number of elements),and as far as I have been digging
into my platform`s library neither of STL containers are built upon
the idea of reference counting .I simply conclude that a pointer to a
container is not really that bad.As a rookie I must respect a pro but
I do not see any reason to complaign about 'vector <T> *' as you did.
The actual container is usually quite small, it's the elements contained
that are large. This means that putting a container on the stack and
inserting thousands of objects is no problem, since the objects are on
the heap. The reason I don't recommend using a pointer to a container
(and if possible not a container with pointers) is that it requires the
user to take care of creating and deleting the container (and also the
elements if using a container of pointers).
Imagine a class Foo like this:
class Foo
{
std::vector<Bar> barContainer;
// ...
};
If used like this I don't have to worry about a thing, I know that all
instances will have a barContainer as soon as they are created without
writing anything in the constructor, and when the object is destroyed so
will the container (again, without putting anything in the destructor).
And, when the vector is destroyed so are the elements it contains.
If you had a pointer to the vector you need to new it in the constructor
and delete it in the destructor, or you'll be leaking memory. If you
have a pointer to the vector I'd take that as a sign that the vector is
not owned by the object, perhaps it's shared between many Foo objects or
some such.
If you have a vector of pointers, you must again implement the
destructor, and this time loop through the vector and delete each and
every element in it, or you would be leaking loads of memory.
In short use only pointers where ownership, lifetime, or efficiency
requires it, because it complicates the code.
--
Erik Wikstr?m