Custom allocator sample code for vector

Discussion in 'C++' started by Alex Vinokur, Aug 6, 2004.

  1. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Aug 6, 2004
    #1
    1. Advertising

  2. Alex Vinokur

    Sharad Kala Guest

    Sharad Kala, Aug 6, 2004
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    "Sharad Kala" <> wrote in message news:...
    >
    > "Alex Vinokur" <> wrote in message
    > news:...
    > > I am looking for any custom allocator sample code for std::vector.
    > >

    >
    > http://www.josuttis.com/libbook/memory/myalloc1.cpp.html
    >
    >


    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
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 6, 2004
    #3
  4. Alex Vinokur

    Sharad Kala Guest

    "Alex Vinokur" <> wrote in message
    news:...

    > When should 'construct' and 'destroy' be invoked?


    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
     
    Sharad Kala, Aug 7, 2004
    #4
  5. Alex Vinokur

    Alex Vinokur Guest

    "Sharad Kala" <> wrote in message news:...
    >
    > "Alex Vinokur" <> wrote in message
    > news:...
    >
    > > When should 'construct' and 'destroy' be invoked?

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


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

    However, in http://www.josuttis.com/libbook/memory/myalloc1.cpp.html construct isn't invoked while push_back invocation.

    > Destroy simply calls the destructor for the object.
    >
    > -Sharad
    >
    >
    >



    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 7, 2004
    #5
  6. Alex Vinokur

    Alex Vinokur Guest

    "Alex Vinokur" <> wrote in message news:...
    >
    > "Sharad Kala" <> wrote in message news:...
    > >
    > > "Alex Vinokur" <> wrote in message
    > > news:...
    > >
    > > > When should 'construct' and 'destroy' be invoked?

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

    >
    > For instance, v.push_back (element) for vector?
    >
    > However, in http://www.josuttis.com/libbook/memory/myalloc1.cpp.html construct isn't invoked while push_back invocation.
    >
    > > Destroy simply calls the destructor for the object.
    > >
    > > -Sharad
    > >


    [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,oldsz,newsz) is added in "SGI", and behaves as
    * if p=realloc(p,newsz).
    *
    * "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_DEPRECATED
    * 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/libstdc /ext/howto.html#3
    */

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


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 7, 2004
    #6
  7. Alex Vinokur

    Sharad Kala Guest

    "Alex Vinokur" <> wrote in message
    news:...
    >
    > For instance, v.push_back (element) for vector?


    Yes

    > However, in http://www.josuttis.com/libbook/memory/myalloc1.cpp.html

    construct isn't invoked while push_back invocation.

    Try putting a breakpoint in Construct and then see.

    -Sharad
     
    Sharad Kala, Aug 7, 2004
    #7
  8. Alex Vinokur

    Alex Vinokur Guest

    "Sharad Kala" <> wrote in message news:...
    >
    > "Alex Vinokur" <> wrote in message
    > news:...
    > >
    > > For instance, v.push_back (element) for vector?

    >
    > Yes
    >
    > > However, in http://www.josuttis.com/libbook/memory/myalloc1.cpp.html

    > construct isn't invoked while push_back invocation.
    >
    > Try putting a breakpoint in Construct and then see.
    >


    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
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 7, 2004
    #8
  9. Alex Vinokur

    Sharad Kala Guest


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


    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.
     
    Sharad Kala, Aug 7, 2004
    #9
  10. Alex Vinokur

    Alex Vinokur Guest

    "Sharad Kala" <> wrote in message news:...
    >
    > >
    > > I put assert(0) in construct().
    > > No response.

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


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


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 7, 2004
    #10
  11. Alex Vinokur

    Alex Vinokur Guest

    "Alex Vinokur" <> wrote in message news:...
    >
    > "Sharad Kala" <> wrote in message news:...
    > >
    > > "Alex Vinokur" <> wrote in message
    > > news:...
    > > >
    > > > For instance, v.push_back (element) for vector?

    > >
    > > Yes
    > >
    > > > However, in http://www.josuttis.com/libbook/memory/myalloc1.cpp.html

    > > construct isn't invoked while push_back invocation.
    > >
    > > Try putting a breakpoint in Construct and then see.
    > >

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

    [snip]

    Different compilers produce quite different behavior with the same custom allocator.

    Here is the Nicolai M. Josuttis' code sample with cosmetic changes added by me to profile the executable:
    http://groups.google.com/groups?threadm=

    Here are log files for the following compilers
    * GNU g++ : http://groups.google.com/groups?selm=
    * Microsoft C++ : http://groups.google.com/groups?selm=
    * Borland C++ : http://groups.google.com/groups?selm=

    We can see that
    * GNU g++ 3.3.1 doesn't invoke construct() and destroy();
    * Microsoft C++ 13.00 and Borland C++ 5.5.1 do invoke construct() and destroy();

    We can see also that behavior Microsoft C++ 13.00 and Borland C++ 5.5.1 is quite different;
    for instance, Borland C++ allocates memory for 256 elements, Microsoft C++ allocates memory for small number of elements.


    So, behavior of a custom allocator is significantly compiler-dependent.
    The question is what can one achieve using a _custom_ allocator?

    The more specific question: do we need construct() and destroy() indeed? For instance, g++ doesn't use them.

    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 10, 2004
    #11
  12. Alex Vinokur

    tom_usenet Guest

    On Fri, 6 Aug 2004 18:56:06 +0300, "Alex Vinokur"
    <> wrote:

    >
    >"Sharad Kala" <> wrote in message news:...
    >>
    >> "Alex Vinokur" <> wrote in message
    >> news:...
    >> > I am looking for any custom allocator sample code for std::vector.
    >> >

    >>
    >> http://www.josuttis.com/libbook/memory/myalloc1.cpp.html
    >>
    >>

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


    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
     
    tom_usenet, Aug 10, 2004
    #12
  13. Alex Vinokur

    Sharad Kala Guest


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


    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 ?
     
    Sharad Kala, Aug 11, 2004
    #13
  14. Alex Vinokur

    tom_usenet Guest

    On Wed, 11 Aug 2004 14:45:24 +0530, "Sharad Kala"
    <> wrote:

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

    >
    >Why are they then as part of standard in allocator requirements in 20.1.5 ?


    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::pointer_type be castable to void*, which is not a good
    requirement to have!

    >Is it an optional feature or are the compilers not being conformant ?


    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
     
    tom_usenet, Aug 11, 2004
    #14
  15. "Alex Vinokur" <> wrote in message news:<>...
    > "Sharad Kala" <> wrote in message news:...
    > >
    > > "Alex Vinokur" <> wrote in message
    > > news:...
    > > > I am looking for any custom allocator sample code for std::vector.
    > > >

    > >
    > > http://www.josuttis.com/libbook/memory/myalloc1.cpp.html
    > >
    > >

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


    '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.
     
    jose luis fernandez diaz, Aug 11, 2004
    #15
  16. Alex Vinokur

    Alex Vinokur Guest

    "jose luis fernandez diaz" <> wrote in message
    news:...
    > "Alex Vinokur" <> wrote in message news:<>...
    > > "Sharad Kala" <> wrote in message news:...
    > > >
    > > > "Alex Vinokur" <> wrote in message
    > > > news:...
    > > > > I am looking for any custom allocator sample code for std::vector.
    > > > >
    > > >
    > > > http://www.josuttis.com/libbook/memory/myalloc1.cpp.html
    > > >
    > > >

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

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

    [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
    http://www.mit.edu/afs/athena/astaff/project/gccdev/egcs/libstdc -v3/include/bits/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_storage)
    {
    _Construct(_M_finish, __x);
    ++_M_finish;
    }
    else
    _M_insert_aux(end(), __x);
    }
    --------- push_back() : END -----------


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 12, 2004
    #16
  17. Alex Vinokur

    Alex Vinokur Guest

    "Alex Vinokur" <> wrote in message news:...
    >

    [snip]
    > Here is the Nicolai M. Josuttis' code sample with cosmetic changes added by me to profile the executable:
    > http://groups.google.com/groups?threadm=
    >
    > Here are log files for the following compilers
    > * GNU g++ : http://groups.google.com/groups?selm=

    [snip]
    >
    > We can see that
    > * GNU g++ 3.3.1 doesn't invoke construct() and destroy();

    [snip]


    "Matt Austern" <> wrote in message
    http://article.gmane.org/gmane.comp.gcc.libstdc .devel/9815

    > On Jul 19, 2004, at 9:25 AM, wrote:
    >
    > > Hi everybody !
    > >
    > > There's a behaviour in STL allocator I don't understand, I hope
    > > somebody can
    > > help me...
    > >
    > > I'm trying to customize an allocator to track the insertion/deletion
    > > of objects
    > > in a container,
    > > decorating the standard allocator functions construct() and destroy().
    > >
    > > The problem is that it seems that those functions are never called in
    > > STL
    > > containers, instead
    > > a global template function _Construct() (defined in <stl_construct.h>)
    > > is
    > > called, that is
    > > completely unaware of allocators.

    >
    > This is a bug.

    [snip]

    See also several relevant threads from news://news.gmane.org/gmane.comp.gcc.libstdc .devel

    The thread titled "Custom allocator for vector and libstdc++-v3"
    http://thread.gmane.org/gmane.comp.gcc.libstdc .devel/10087

    The thread titled "allocator construct() / destruct() behaviour..."
    http://thread.gmane.org/gmane.comp.gcc.libstdc .devel/9814

    The thread titled "PATCH: use construct and destroy from user-provided allocators"
    http://thread.gmane.org/gmane.comp.gcc.libstdc .devel/9847


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 16, 2004
    #17
    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. Gernot Frisch

    std::vector allocator?

    Gernot Frisch, Jul 21, 2004, in forum: C++
    Replies:
    4
    Views:
    3,279
    Gernot Frisch
    Jul 21, 2004
  2. Replies:
    8
    Views:
    1,983
    Csaba
    Feb 18, 2006
  3. Alex Vinokur
    Replies:
    3
    Views:
    479
    Kai-Uwe Bux
    Apr 1, 2006
  4. Replies:
    3
    Views:
    3,173
  5. z65536
    Replies:
    4
    Views:
    444
    red floyd
    Jul 6, 2010
Loading...

Share This Page