473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

std::vector <mytype> allocation strategy

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
Jul 22 '05 #1
11 1898
Lynn wrote:
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
Jul 22 '05 #2
"Lynn" <NO****@NOSPAM.com> wrote in message
news:cp********@library1.airnews.net...
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.
Jul 22 '05 #3
>> 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.


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
Jul 22 '05 #4
Lynn wrote:
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
Jul 22 '05 #5

"Default User" <fi********@boeing.com.invalid> wrote in message
news:I8********@news.boeing.com...
Lynn wrote:
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.


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
Jul 22 '05 #6
Mike Wahler wrote:

"Default User" <fi********@boeing.com.invalid> wrote in message
news:I8********@news.boeing.com...

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.

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
Jul 22 '05 #7
"Default User" <fi********@boeing.com.invalid> wrote in message
news:I8********@news.boeing.com...
Mike Wahler wrote:

"Default User" <fi********@boeing.com.invalid> wrote in message
news:I8********@news.boeing.com...

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.

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
Jul 22 '05 #8
Mike Wahler wrote:
"Default User" <fi********@boeing.com.invalid> wrote in message
news:I8********@news.boeing.com...
Mike Wahler wrote:

"Default User" <fi********@boeing.com.invalid> wrote in message
news:I8********@news.boeing.com...

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.

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


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

V
Jul 22 '05 #9
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:_X***************@newsread1.dllstx09.us.to.ve rio.net...
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...


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

Jul 22 '05 #10

Victor Bazarov wrote:
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

Jul 22 '05 #11
msalters wrote:

Victor Bazarov wrote:
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.

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


Brian
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: barcaroller | last post by:
I have a map<T*that stores pointers to objects. How can I tell map<T*to use the objects' operator<() and not the value of the pointers for sorting? If that's not feasible, what alternatives do...
6
by: muzicmakr | last post by:
I'm porting some code from windows to mac, and there are some instances of std::vector<const MyType>, that compiled just fine on the pc, but won't compile under gcc. I'd never tried to do this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.