std::vector <mytype> allocation strategy

Discussion in 'C++' started by Lynn, Dec 14, 2004.

  1. Lynn

    Lynn Guest

    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
    Lynn, Dec 14, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Dec 14, 2004
    #2
    1. Advertising

  3. "Lynn" <> wrote in message
    news:cpn3ev$...

    > 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.
    Andrew Koenig, Dec 14, 2004
    #3
  4. Lynn

    Lynn Guest

    >> 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
    Lynn, Dec 14, 2004
    #4
  5. Lynn

    Default User Guest

    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
    Default User, Dec 14, 2004
    #5
  6. Lynn

    Mike Wahler Guest

    "Default User" <> wrote in message
    news:...
    > 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
    Mike Wahler, Dec 14, 2004
    #6
  7. Lynn

    Default User Guest

    Mike Wahler wrote:

    >
    > "Default User" <> wrote in message
    > news:...


    > > 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
    Default User, Dec 14, 2004
    #7
  8. Lynn

    Mike Wahler Guest

    "Default User" <> wrote in message
    news:...
    > Mike Wahler wrote:
    >
    > >
    > > "Default User" <> wrote in message
    > > news:...

    >
    > > > 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
    Mike Wahler, Dec 14, 2004
    #8
  9. Mike Wahler wrote:
    > "Default User" <> wrote in message
    > news:...
    >
    >>Mike Wahler wrote:
    >>
    >>
    >>>"Default User" <> wrote in message
    >>>news:...

    >>
    >>>>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
    Victor Bazarov, Dec 14, 2004
    #9
  10. Lynn

    Mike Wahler Guest

    "Victor Bazarov" <> wrote in message
    news:_XKvd.12330$09.us.to.verio.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
    Mike Wahler, Dec 15, 2004
    #10
  11. Lynn

    msalters Guest

    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
    msalters, Dec 15, 2004
    #11
  12. Lynn

    Default User Guest

    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
    Default User, Dec 15, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    20
    Views:
    4,269
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    707
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    4
    Views:
    354
  4. Replies:
    2
    Views:
    526
    Andrew Koenig
    Feb 9, 2009
  5. daniele.g
    Replies:
    4
    Views:
    475
    Joe Pfeiffer
    Jul 15, 2011
Loading...

Share This Page