std::vector <mytype> allocation strategy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lynn

    #1

    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


  • Victor Bazarov

    #2
    Re: std::vector &lt;mytype&g t; allocation strategy

    Lynn wrote:[color=blue]
    > 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 ?[/color]

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

    V

    Comment

    • Andrew Koenig

      #3
      Re: std::vector &lt;mytype&g t; allocation strategy

      "Lynn" <NOSPAM@NOSPAM. com> wrote in message
      news:cpn3ev$5m4 @library1.airne ws.net...
      [color=blue]
      > 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 ?[/color]

      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.


      Comment

      • Lynn

        #4
        Re: std::vector &lt;mytype&g t; allocation strategy

        >> Is it better to use std::vector <type> with a pointer to the types being[color=blue][color=green]
        >> stored or the actual storage for the types ? In other words, is
        >> std::vector <mytype> or std::vector <mytype *> better ?[/color]
        >
        > 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.[/color]

        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


        Comment

        • Default User

          #5
          Re: std::vector &lt;mytype&g t; allocation strategy

          Lynn wrote:
          [color=blue]
          > 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 ?[/color]


          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

          Comment

          • Mike Wahler

            #6
            Re: std::vector &lt;mytype&g t; allocation strategy


            "Default User" <first.last@boe ing.com.invalid > wrote in message
            news:I8q6vE.K1z @news.boeing.co m...[color=blue]
            > Lynn wrote:
            >[color=green]
            > > 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 ?[/color]
            >
            >
            > Who really cares about some mythical "general" case? There are only
            > specific cases. You use the form the particular case calls for.[/color]

            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


            Comment

            • Default User

              #7
              Re: std::vector &lt;mytype&g t; allocation strategy

              Mike Wahler wrote:
              [color=blue]
              >
              > "Default User" <first.last@boe ing.com.invalid > wrote in message
              > news:I8q6vE.K1z @news.boeing.co m...[/color]
              [color=blue][color=green]
              > > Who really cares about some mythical "general" case? There are only
              > > specific cases. You use the form the particular case calls for.[/color]
              >
              > 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.[/color]


              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

              Comment

              • Mike Wahler

                #8
                Re: std::vector &lt;mytype&g t; allocation strategy

                "Default User" <first.last@boe ing.com.invalid > wrote in message
                news:I8qEF6.5Gq @news.boeing.co m...[color=blue]
                > Mike Wahler wrote:
                >[color=green]
                > >
                > > "Default User" <first.last@boe ing.com.invalid > wrote in message
                > > news:I8q6vE.K1z @news.boeing.co m...[/color]
                >[color=green][color=darkred]
                > > > Who really cares about some mythical "general" case? There are only
                > > > specific cases. You use the form the particular case calls for.[/color]
                > >
                > > 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.[/color]
                >
                >
                > 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).[/color]

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

                -Mike


                Comment

                • Victor Bazarov

                  #9
                  Re: std::vector &lt;mytype&g t; allocation strategy

                  Mike Wahler wrote:[color=blue]
                  > "Default User" <first.last@boe ing.com.invalid > wrote in message
                  > news:I8qEF6.5Gq @news.boeing.co m...
                  >[color=green]
                  >>Mike Wahler wrote:
                  >>
                  >>[color=darkred]
                  >>>"Default User" <first.last@boe ing.com.invalid > wrote in message
                  >>>news:I8q6vE. K1z@news.boeing .com...[/color]
                  >>[color=darkred]
                  >>>>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.[/color]
                  >>
                  >>
                  >>That's pretty good advice, in general :)
                  >>
                  >>As I said, the main reason to use pointers at all in C++ is for
                  >>polymorphis m (try to splel it right this time).[/color]
                  >
                  >
                  > I try to use references instead of pointers when implementing
                  > polymorphism. Not always practical, but it's my 'default'.[/color]

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

                  V

                  Comment

                  • Mike Wahler

                    #10
                    Re: std::vector &lt;mytype&g t; allocation strategy

                    "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                    news:_XKvd.1233 0$Ae.11@newsrea d1.dllstx09.us. to.verio.net...[color=blue][color=green]
                    > > I try to use references instead of pointers when implementing
                    > > polymorphism. Not always practical, but it's my 'default'.[/color]
                    >
                    > Tough to use references when you need an array or a container of your
                    > polymorphic objects...[/color]

                    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



                    Comment

                    • msalters

                      #11
                      Re: std::vector &lt;mytype&g t; allocation strategy


                      Victor Bazarov wrote:
                      [color=blue]
                      > Tough to use references when you need an array or a container of your
                      > polymorphic objects...[/color]

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

                      Comment

                      • Default User

                        #12
                        Re: std::vector &lt;mytype&g t; allocation strategy

                        msalters wrote:
                        [color=blue]
                        >
                        > Victor Bazarov wrote:
                        >[color=green]
                        > > Tough to use references when you need an array or a container of
                        > > your polymorphic objects...[/color]
                        >
                        > std::vector<boo st::ref<T> > sounds like the solution to that.[/color]


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




                        Brian

                        Comment

                        Working...