Custom allocator sample code for vector

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

    Custom allocator sample code for vector

    I am looking for any custom allocator sample code for std::vector.

    Thanks.

    --
    Alex Vinokur







  • Sharad Kala

    #2
    Re: Custom allocator sample code for vector


    "Alex Vinokur" <alexvn@big-foot.com> wrote in message
    news:2ngusqFp3p gU1@uni-berlin.de...[color=blue]
    > I am looking for any custom allocator sample code for std::vector.
    >[/color]

    Nicolai Josuttis, The C++ Standard Library - A Tutorial and Reference, allocator, user-defined, usage



    Comment

    • Alex Vinokur

      #3
      Re: Custom allocator sample code for vector


      "Sharad Kala" <no__spam.shara dk_ind@yahoo.co m> wrote in message news:2ngv12Fo88 bU1@uni-berlin.de...[color=blue]
      >
      > "Alex Vinokur" <alexvn@big-foot.com> wrote in message
      > news:2ngusqFp3p gU1@uni-berlin.de...[color=green]
      > > I am looking for any custom allocator sample code for std::vector.
      > >[/color]
      >
      > http://www.josuttis.com/libbook/memo...lloc1.cpp.html
      >
      >[/color]

      Thanks.

      In that sample we can see that methods 'allocate' and 'deallocate' were invoked.
      But methods 'construct' and 'destroy' were not invoked.
      When should 'construct' and 'destroy' be invoked?


      --
      Alex Vinokur







      Comment

      • Sharad Kala

        #4
        Re: Custom allocator sample code for vector


        "Alex Vinokur" <alexvn@big-foot.com> wrote in message
        news:2nho0uF105 teU1@uni-berlin.de...
        [color=blue]
        > When should 'construct' and 'destroy' be invoked?[/color]

        An allocator has to provide a construct and destruct operation. You may want
        to take a look at 20.1.5 (Allocator requirements).

        Construct is used to initialize the memory allocated with a value. This is
        basically a call to placement new. So basically construct is invoked when an
        object is created in the container.
        Destroy simply calls the destructor for the object.

        -Sharad



        Comment

        • Alex Vinokur

          #5
          Re: Custom allocator sample code for vector


          "Sharad Kala" <no__spam.shara dk_ind@yahoo.co m> wrote in message news:2njcgfF1iu 4fU1@uni-berlin.de...[color=blue]
          >
          > "Alex Vinokur" <alexvn@big-foot.com> wrote in message
          > news:2nho0uF105 teU1@uni-berlin.de...
          >[color=green]
          > > When should 'construct' and 'destroy' be invoked?[/color]
          >
          > An allocator has to provide a construct and destruct operation. You may want
          > to take a look at 20.1.5 (Allocator requirements).
          >
          > Construct is used to initialize the memory allocated with a value. This is
          > basically a call to placement new. So basically construct is invoked when an
          > object is created in the container.[/color]

          For instance, v.push_back (element) for vector?

          However, in http://www.josuttis.com/libbook/memo...lloc1.cpp.html construct isn't invoked while push_back invocation.
          [color=blue]
          > Destroy simply calls the destructor for the object.
          >
          > -Sharad
          >
          >
          >[/color]


          --
          Alex Vinokur





          Comment

          • Alex Vinokur

            #6
            Re: Custom allocator sample code for vector


            "Alex Vinokur" <alexvn@big-foot.com> wrote in message news:2njsjeF1lj vrU1@uni-berlin.de...[color=blue]
            >
            > "Sharad Kala" <no__spam.shara dk_ind@yahoo.co m> wrote in message news:2njcgfF1iu 4fU1@uni-berlin.de...[color=green]
            > >
            > > "Alex Vinokur" <alexvn@big-foot.com> wrote in message
            > > news:2nho0uF105 teU1@uni-berlin.de...
            > >[color=darkred]
            > > > When should 'construct' and 'destroy' be invoked?[/color]
            > >
            > > An allocator has to provide a construct and destruct operation. You may want
            > > to take a look at 20.1.5 (Allocator requirements).
            > >
            > > Construct is used to initialize the memory allocated with a value. This is
            > > basically a call to placement new. So basically construct is invoked when an
            > > object is created in the container.[/color]
            >
            > For instance, v.push_back (element) for vector?
            >
            > However, in http://www.josuttis.com/libbook/memo...lloc1.cpp.html construct isn't invoked while push_back invocation.
            >[color=green]
            > > Destroy simply calls the destructor for the object.
            > >
            > > -Sharad
            > >[/color][/color]

            [snip]


            Compiler g++ 3.3.1 (cygming special).
            Here is a fragment from file stl_alloc.h.

            /**
            * @defgroup Allocators Memory Allocators
            * @if maint
            * stl_alloc.h implements some node allocators. These are NOT the same as
            * allocators in the C++ standard, nor in the original H-P STL. They do not
            * encapsulate different pointer types; we assume that there is only one
            * pointer type. The C++ standard allocators are intended to allocate
            * individual objects, not pools or arenas.
            *
            * In this file allocators are of two different styles: "standard" and
            * "SGI" (quotes included). "Standard" allocators conform to 20.4. "SGI"
            * allocators differ in AT LEAST the following ways (add to this list as you
            * discover them):
            *
            * - "Standard" allocate() takes two parameters (n_count,hint=0 ) but "SGI"
            * allocate() takes one paramter (n_size).
            * - Likewise, "standard" deallocate()'s argument is a count, but in "SGI"
            * is a byte size.
            * - max_size(), construct(), and destroy() are missing in "SGI" allocators.
            * - reallocate(p,ol dsz,newsz) is added in "SGI", and behaves as
            * if p=realloc(p,new sz).
            *
            * "SGI" allocators may be wrapped in __allocator to convert the interface
            * into a "standard" one.
            * @endif
            *
            * @note The @c reallocate member functions have been deprecated for 3.2
            * and will be removed in 3.4. You must define @c _GLIBCPP_DEPREC ATED
            * to make this visible in 3.2; see c++config.h.
            *
            * The canonical description of these classes is in docs/html/ext/howto.html
            * or online at http://gcc.gnu.org/onlinedocs/libstd...t/howto.html#3
            */

            Perhaps, MyAlloc in the Josuttis' sample works as "SGI" allocator which doesn't contain construct(), and destroy() (?).


            --
            Alex Vinokur





            Comment

            • Sharad Kala

              #7
              Re: Custom allocator sample code for vector


              "Alex Vinokur" <alexvn@big-foot.com> wrote in message
              news:2njsjeF1lj vrU1@uni-berlin.de...[color=blue]
              >
              > For instance, v.push_back (element) for vector?[/color]

              Yes
              [color=blue]
              > However, in http://www.josuttis.com/libbook/memo...lloc1.cpp.html[/color]
              construct isn't invoked while push_back invocation.

              Try putting a breakpoint in Construct and then see.

              -Sharad



              Comment

              • Alex Vinokur

                #8
                Re: Custom allocator sample code for vector


                "Sharad Kala" <no__spam.shara dk_ind@yahoo.co m> wrote in message news:2nju1qF1ib phU1@uni-berlin.de...[color=blue]
                >
                > "Alex Vinokur" <alexvn@big-foot.com> wrote in message
                > news:2njsjeF1lj vrU1@uni-berlin.de...[color=green]
                > >
                > > For instance, v.push_back (element) for vector?[/color]
                >
                > Yes
                >[color=green]
                > > However, in http://www.josuttis.com/libbook/memo...lloc1.cpp.html[/color]
                > construct isn't invoked while push_back invocation.
                >
                > Try putting a breakpoint in Construct and then see.
                >[/color]

                I put assert(0) in construct().
                No response.

                // initialize elements of allocated storage p with value value
                void construct (pointer p, const T& value)
                {
                // initialize memory with placement new
                new((void*)p)T( value);
                assert (0); // Added by me
                }

                [snip]


                --
                Alex Vinokur






                Comment

                • Sharad Kala

                  #9
                  Re: Custom allocator sample code for vector

                  [color=blue]
                  >
                  > I put assert(0) in construct().
                  > No response.[/color]

                  Is NDEBUG macro defined ? I can very much see the assert failure on MS VC 7
                  if I insert the above line (in debug build). Try putting cout << "In
                  construct" and then see.


                  Comment

                  • Alex Vinokur

                    #10
                    Re: Custom allocator sample code for vector


                    "Sharad Kala" <no__spam.shara dk_ind@yahoo.co m> wrote in message news:2njv3nF1lp auU1@uni-berlin.de...[color=blue]
                    >[color=green]
                    > >
                    > > I put assert(0) in construct().
                    > > No response.[/color]
                    >
                    > Is NDEBUG macro defined ? I can very much see the assert failure on MS VC 7
                    > if I insert the above line (in debug build). Try putting cout << "In
                    > construct" and then see.
                    >
                    >[/color]

                    Done: cout << "In construct" has been put.
                    No response too.
                    It seems that construct() is not invoked.


                    --
                    Alex Vinokur





                    Comment

                    • tom_usenet

                      #11
                      Re: Custom allocator sample code for vector

                      On Fri, 6 Aug 2004 18:56:06 +0300, "Alex Vinokur"
                      <alexvn@big-foot.com> wrote:
                      [color=blue]
                      >
                      >"Sharad Kala" <no__spam.shara dk_ind@yahoo.co m> wrote in message news:2ngv12Fo88 bU1@uni-berlin.de...[color=green]
                      >>
                      >> "Alex Vinokur" <alexvn@big-foot.com> wrote in message
                      >> news:2ngusqFp3p gU1@uni-berlin.de...[color=darkred]
                      >> > I am looking for any custom allocator sample code for std::vector.
                      >> >[/color]
                      >>
                      >> http://www.josuttis.com/libbook/memo...lloc1.cpp.html
                      >>
                      >>[/color]
                      >
                      >Thanks.
                      >
                      >In that sample we can see that methods 'allocate' and 'deallocate' were invoked.
                      >But methods 'construct' and 'destroy' were not invoked.
                      >When should 'construct' and 'destroy' be invoked?[/color]

                      They may never be invoked - containers are not required to call them.
                      I believe that Dinkumware's lib does call them, since they support
                      "unusual" pointer types and memory models. I doubt anyone else does.

                      Tom

                      Comment

                      • Sharad Kala

                        #12
                        Re: Custom allocator sample code for vector

                        [color=blue]
                        > They may never be invoked - containers are not required to call them.
                        > I believe that Dinkumware's lib does call them, since they support
                        > "unusual" pointer types and memory models. I doubt anyone else does.[/color]

                        Why are they then as part of standard in allocator requirements in 20.1.5 ?
                        Is it an optional feature or are the compilers not being conformant ?


                        Comment

                        • tom_usenet

                          #13
                          Re: Custom allocator sample code for vector

                          On Wed, 11 Aug 2004 14:45:24 +0530, "Sharad Kala"
                          <no__spam.shara dk_ind@yahoo.co m> wrote:
                          [color=blue]
                          >[color=green]
                          >> They may never be invoked - containers are not required to call them.
                          >> I believe that Dinkumware's lib does call them, since they support
                          >> "unusual" pointer types and memory models. I doubt anyone else does.[/color]
                          >
                          >Why are they then as part of standard in allocator requirements in 20.1.5 ?[/color]

                          I think the original intention was that they were there to allow
                          pointer types that aren't T*. However, the standard says for
                          construct:

                          Effect: new ((void*)p) T(t)

                          I think that is a defect, since it requires that
                          allocator::poin ter_type be castable to void*, which is not a good
                          requirement to have!
                          [color=blue]
                          >Is it an optional feature or are the compilers not being conformant ?[/color]

                          It's an optional feature whether or not the containers use it or not -
                          I don't see any mention of whether containers must call it or not,
                          which I think makes it optional.

                          Tom

                          Comment

                          • jose luis fernandez diaz

                            #14
                            Re: Custom allocator sample code for vector

                            "Alex Vinokur" <alexvn@big-foot.com> wrote in message news:<2nho0uF10 5teU1@uni-berlin.de>...[color=blue]
                            > "Sharad Kala" <no__spam.shara dk_ind@yahoo.co m> wrote in message news:2ngv12Fo88 bU1@uni-berlin.de...[color=green]
                            > >
                            > > "Alex Vinokur" <alexvn@big-foot.com> wrote in message
                            > > news:2ngusqFp3p gU1@uni-berlin.de...[color=darkred]
                            > > > I am looking for any custom allocator sample code for std::vector.
                            > > >[/color]
                            > >
                            > > http://www.josuttis.com/libbook/memo...lloc1.cpp.html
                            > >
                            > >[/color]
                            >
                            > Thanks.
                            >
                            > In that sample we can see that methods 'allocate' and 'deallocate' were invoked.
                            > But methods 'construct' and 'destroy' were not invoked.
                            > When should 'construct' and 'destroy' be invoked?[/color]

                            'allocate'only reserves raw memory. Wnen you insert objects in that
                            allocated memory, for example with vector 'push_back' method,
                            'construct' method must be invoked. It is invoked from vector
                            'push_back' method. Can you post vector 'push_back' method code from
                            the vector library file that you are using ?

                            Regards,
                            Jose Luis.

                            Comment

                            • Alex Vinokur

                              #15
                              Re: Custom allocator sample code for vector


                              "jose luis fernandez diaz" <jose_luis_fdez _diaz_news@yaho o.es> wrote in message
                              news:c2f95fd0.0 408110938.1a5ab 7bd@posting.goo gle.com...[color=blue]
                              > "Alex Vinokur" <alexvn@big-foot.com> wrote in message news:<2nho0uF10 5teU1@uni-berlin.de>...[color=green]
                              > > "Sharad Kala" <no__spam.shara dk_ind@yahoo.co m> wrote in message news:2ngv12Fo88 bU1@uni-berlin.de...[color=darkred]
                              > > >
                              > > > "Alex Vinokur" <alexvn@big-foot.com> wrote in message
                              > > > news:2ngusqFp3p gU1@uni-berlin.de...
                              > > > > I am looking for any custom allocator sample code for std::vector.
                              > > > >
                              > > >
                              > > > http://www.josuttis.com/libbook/memo...lloc1.cpp.html
                              > > >
                              > > >[/color]
                              > >
                              > > Thanks.
                              > >
                              > > In that sample we can see that methods 'allocate' and 'deallocate' were invoked.
                              > > But methods 'construct' and 'destroy' were not invoked.
                              > > When should 'construct' and 'destroy' be invoked?[/color]
                              >
                              > 'allocate'only reserves raw memory. Wnen you insert objects in that
                              > allocated memory, for example with vector 'push_back' method,
                              > 'construct' method must be invoked. It is invoked from vector
                              > 'push_back' method. Can you post vector 'push_back' method code from
                              > the vector library file that you are using ?
                              >[/color]
                              [snip]

                              --------------------------------------------
                              GNU g++ version 3.3.1 (cygming special).
                              GNU Standard C++ Library libstdc++-v3
                              --------------------------------------------

                              Here is the push_back() method from file stl_vector.h



                              --------- push_back() : BEGIN ---------
                              // [23.2.4.3] modifiers
                              /**
                              * @brief Add data to the end of the %vector.
                              * @param x Data to be added.
                              *
                              * This is a typical stack operation. The function creates an element at
                              * the end of the %vector and assigns the given data to it.
                              * Due to the nature of a %vector this operation can be done in constant
                              * time if the %vector has preallocated space available.
                              */
                              void
                              push_back(const value_type& __x)
                              {
                              if (_M_finish != _M_end_of_stora ge)
                              {
                              _Construct(_M_f inish, __x);
                              ++_M_finish;
                              }
                              else
                              _M_insert_aux(e nd(), __x);
                              }
                              --------- push_back() : END -----------


                              --
                              Alex Vinokur






                              Comment

                              Working...