std::vector <mytype> allocation strategy

L

Lynn

Hi,

Is it better to use std::vector <type> with a pointer to the types being
stored or the actual storage for the types ? In other words, is
std::vector <mytype> or std::vector <mytype *> better ? I know that
there are specific cases where it is dictated but in the general case,
what do you think ?

Thanks,
Lynn McGuire
 
V

Victor Bazarov

Lynn said:
Is it better to use std::vector <type> with a pointer to the types being
stored or the actual storage for the types ? In other words, is
std::vector <mytype> or std::vector <mytype *> better ? I know that
there are specific cases where it is dictated but in the general case,
what do you think ?

There can be no other answer than "it depends". There is no "general
case" for this.

V
 
A

Andrew Koenig

Is it better to use std::vector <type> with a pointer to the types being
stored or the actual storage for the types ? In other words, is
std::vector <mytype> or std::vector <mytype *> better ?

If you have to ask, then the answer is that it is better not to use vectors
of pointers. My rationale for this answer is that if you use pointers, you
take on responsibility for managing the objects to which the pointers point.
If you knew everything you need to know in order to discharge this
responsibility, you probably wouldn't need to ask the question in the first
place.
 
L

Lynn

Is it better to use std::vector said:
If you have to ask, then the answer is that it is better not to use vectors of pointers. My rationale for this answer is that if
you use pointers, you take on responsibility for managing the objects to which the pointers point. If you knew everything you need
to know in order to discharge this responsibility, you probably wouldn't need to ask the question in the first place.

Yup, that is what I am thinking. When one uses the automatic memory
mangement built into vector, one gains a wide experience and breadth
of well working and debugged code. Why duplicate this functionality
unless your code already expects to have null pointers in the vector
(which my current code that I am upgrading does not).

Thanks,
Lynn
 
D

Default User

Lynn said:
Hi,

Is it better to use std::vector <type> with a pointer to the types
being stored or the actual storage for the types ? In other words, is
std::vector <mytype> or std::vector <mytype *> better ? I know that
there are specific cases where it is dictated but in the general case,
what do you think ?


Who really cares about some mythical "general" case? There are only
specific cases. You use the form the particular case calls for.

One big determining factor is whether you'll need polymorpism. If you
need a container that has a collection of objects related by
inheritance, say created with the Factory Pattern, then you need
pointers.

Of course, that means you are responsible for making sure proper
destruction takes place. When I've had to do that, I usually make the
container a member of a manager class, and its destructor will make
sure to delete all the container pointers. There are problems with this
sort of thing, and you should be somewhat cautious about doing it this
way unless you know what you are doing AND have a good reason. There
are some smart pointers that can work as well, but they aren't yet part
of standard C++.



Brian
 
M

Mike Wahler

Default User said:
Who really cares about some mythical "general" case? There are only
specific cases. You use the form the particular case calls for.

Yes, I agree with this. But I'll offer what I think is decent
'general' advice for C++: Only use pointers when you must,
and can really justify them.

-Mike
 
D

Default User

Mike said:
Yes, I agree with this. But I'll offer what I think is decent
'general' advice for C++: Only use pointers when you must,
and can really justify them.


That's pretty good advice, in general :)

As I said, the main reason to use pointers at all in C++ is for
polymorphism (try to splel it right this time).



Brian
 
M

Mike Wahler

Default User said:
That's pretty good advice, in general :)

As I said, the main reason to use pointers at all in C++ is for
polymorphism (try to splel it right this time).

I try to use references instead of pointers when implementing
polymorphism. Not always practical, but it's my 'default'.

-Mike
 
V

Victor Bazarov

Mike said:
I try to use references instead of pointers when implementing
polymorphism. Not always practical, but it's my 'default'.

Tough to use references when you need an array or a container of your
polymorphic objects...

V
 
M

Mike Wahler

Victor Bazarov said:
Tough to use references when you need an array or a container of your
polymorphic objects...

Like I said, "not always practical." :) And in the case of
containers or arrays of pointers, I'd advise an appropriate
'smart pointer' (as I'm sure you know, 'std::auto_ptr' does
not qualify).

-Mike
 
M

msalters

Victor said:
Tough to use references when you need an array or a container of your
polymorphic objects...

std::vector<boost::ref<T> > sounds like the solution to that.
Regards,
Michiel Salters
 
D

Default User

msalters said:
std::vector<boost::ref<T> > sounds like the solution to that.


Not part of standard C++, therefore unavailable to those of us using
certified tool sets.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top