Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Should I use pointer inside container?

Reply
Thread Tools

Should I use pointer inside container?

 
 
Goran
Guest
Posts: n/a
 
      09-08-2007
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;

How works such a stl container? Does it use heap or stack?

Thanks

Goran

 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      09-08-2007
Goran wrote:
> Hi @ all!
>
> Again one small question due to my shakiness of what to use...
>
> What is better / smarter?


STL contains requires the element to be assignable
as it copy each element you push_xxxx and insert.

So, if your element is not assignable, then you have no other way than
to use pointer as element

when your element is assignable, then you have too choose, the major
principle is that whether your element is big object.
if is then use pointer to avoid copy

>
> private:
> vector<MyClass_t> * itsVector;
>
> OR...
>
> private:
> vector<MyClass_t * > * itsVector;
>
> How works such a stl container? Does it use heap or stack?


you have to refer to a C++ text book or check out some implementation code.


--
Thanks
Barry
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      09-08-2007
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
 
Reply With Quote
 
er
Guest
Posts: n/a
 
      09-09-2007
On Sep 8, 10:04 am, Goran <(E-Mail Removed)> 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;
>
> How works such a stl container? Does it use heap or stack?
>
> Thanks
>
> Goran


quickly read this but you might want to refer to a previous post of
mine:
http://groups.google.com/group/comp....9d782f6d6dae52

also google ptr_vector

 
Reply With Quote
 
Saeed Amrollahi
Guest
Posts: n/a
 
      09-09-2007
On Sep 8, 5:04 pm, Goran <(E-Mail Removed)> 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;
>
> How works such a stl container? Does it use heap or stack?
>
> Thanks
>
> Goran


Hi goran
In addition to the answers by others, I want to say something about
vector<MyClass_t * > itsVector;
sometimes, it depends on your design. For example, if MyClass_t is an
abstract class like Shape in graphics or Piece in Chess, you should
use vector of pointers:
vector<Shape> v; // wrong!
vector<Shape *> v; // ok!
Finally, there should be good reasons for using pointer instead of
plain object.

Regards,
Saeed

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      09-09-2007
On Sep 8, 5:27 pm, Erik Wikström <(E-Mail Removed)> 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.

regards,
FM.

 
Reply With Quote
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      09-09-2007
On 2007-09-09 13:34, terminator wrote:
> On Sep 8, 5:27 pm, Erik Wikström <(E-Mail Removed)> 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
 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      09-09-2007

terminator wrote:


> 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.


Then you should have also seen that the contents of containers most
often live on the heap, making there size not much larger than a
pointer.

> I simply conclude that a pointer to a
> container is not really that bad.


Containers already contain pointers. Apart from the fact that one
needs
to manage a resource that was previously managed automagically,
one also has an additional level of indirection for (in the most
cases)
no reason what-so-ever. This is an example of pre-mature
pessimization.

Regards,

Werner

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      09-10-2007
On Sep 9, 3:23 pm, Erik Wikström <(E-Mail Removed)> wrote:
> On 2007-09-09 13:34, terminator wrote:
>
>
>
>
>
> > On Sep 8, 5:27 pm, Erik Wikström <(E-Mail Removed)> 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.


> In short use only pointers where ownership, lifetime, or efficiency
> requires it, because it complicates the code.


thats exacltly the point,when you need to share a container in
different places, or very simpler the return of a function(this one
mabye bad design).

regards,
FM.

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      09-10-2007
On Sep 9, 11:09 pm, werasm <(E-Mail Removed)> wrote:
> terminator wrote:
> > 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.

>
> Then you should have also seen that the contents of containers most
> often live on the heap, making there size not much larger than a
> pointer.
>


I said huge because a container contains data spatered all over the
heap and when it comes to copy(instead of reference) ,then it does not
matter if the data is stored in a contigeous portion of memory or
segmented into peices on different corners of memory.the actual size
of a container is often much larger than its own data structure(one or
two pointers and intrinsic values as well as the vtable in runtime
polymorphic ones).

regards,
FM.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I think argv[i] should give us a pointer value. Why it is given thevalue pointed by pointer tahir rauf C Programming 12 04-29-2011 11:44 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
use pointer and not use pointer, which is faster to access data? shuisheng C++ 4 09-26-2006 08:05 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments