Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::vector <mytype> allocation strategy

Reply
Thread Tools

std::vector <mytype> allocation strategy

 
 
Lynn
Guest
Posts: n/a
 
      12-14-2004
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-14-2004
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
 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      12-14-2004
"Lynn" <(E-Mail Removed)> wrote in message
news:cpn3ev$(E-Mail Removed)...

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


 
Reply With Quote
 
Lynn
Guest
Posts: n/a
 
      12-14-2004
>> 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


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-14-2004
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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-14-2004

"Default User" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-14-2004
Mike Wahler wrote:

>
> "Default User" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...


> > 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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-14-2004
"Default User" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Wahler wrote:
>
> >
> > "Default User" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...

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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-14-2004
Mike Wahler wrote:
> "Default User" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>Mike Wahler wrote:
>>
>>
>>>"Default User" <(E-Mail Removed)> wrote in message
>>>news:(E-Mail Removed)...

>>
>>>>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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-15-2004
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:_XKvd.12330$(E-Mail Removed)09.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



 
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
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
what's your 70-300 study strategy Peng Te MCSD 3 02-05-2004 07:30 PM
MCAD .net job hunting strategy Frank MCSD 1 07-09-2003 01:28 PM



Advertisments