allocator for map

Discussion in 'C++' started by Fred Zwarts, Oct 3, 2005.

  1. Fred Zwarts

    Fred Zwarts Guest

    I think I am missing something obvious. Maybe someone can set me on the right track.

    For some reason (not to be discussed here) I cannot use the normal operator new
    for a certain map, so I want to replace the memory allocation for this particular
    map. I have the impression that the template parameter Allocator is there for this
    purpose.

    A map is declared as

    template <class Key, class T, class Compare = less<Key>
    class Allocator = allocator<T> >
    class map;

    I don't understand the allocator<T> part of it.
    I think that a map needs to allocate more that only objects of class T.
    I assume that elements of a map not only contain an object of
    class T, but also an object of class K and probably also some
    pointers to other elements.
    So, in my understanding not only objects of class T need an allocator,
    but there needs to be an allocator for a complete element of a map.

    Or is a map designed in such a way that only the allocation of class T
    objects can be replaced, but that the allocation of other parts of the
    elements are always performed using the standard operator new?
    In this case replacing the allocator would not be the solution for my problem.

    F.Z.
     
    Fred Zwarts, Oct 3, 2005
    #1
    1. Advertising

  2. Fred Zwarts

    mlimber Guest

    Fred Zwarts wrote:
    > I think I am missing something obvious. Maybe someone can set me on the right track.
    >
    > For some reason (not to be discussed here) I cannot use the normal operator new
    > for a certain map, so I want to replace the memory allocation for this particular
    > map. I have the impression that the template parameter Allocator is there for this
    > purpose.
    >
    > A map is declared as
    >
    > template <class Key, class T, class Compare = less<Key>
    > class Allocator = allocator<T> >
    > class map;
    >
    > I don't understand the allocator<T> part of it.
    > I think that a map needs to allocate more that only objects of class T.
    > I assume that elements of a map not only contain an object of
    > class T, but also an object of class K and probably also some
    > pointers to other elements.
    > So, in my understanding not only objects of class T need an allocator,
    > but there needs to be an allocator for a complete element of a map.
    >
    > Or is a map designed in such a way that only the allocation of class T
    > objects can be replaced, but that the allocation of other parts of the
    > elements are always performed using the standard operator new?
    > In this case replacing the allocator would not be the solution for my problem.
    >
    > F.Z.


    I presume you have good book on C++ (e.g., Stroustrup's _The C++
    Programming Language_ 3rd ed.). Allocators are a less frequently used
    part of C++, and I would fear implementing my own without a reliable
    guide. Anywho, if you have the mentioned book, section 19.4.1 discusses
    the rebind template in the standard allocator which allows allocator<T>
    to be used for other types than T. In short, I think your desired
    approach can work.

    Cheers! --M
     
    mlimber, Oct 3, 2005
    #2
    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. Dan
    Replies:
    0
    Views:
    468
  2. Fraser Ross

    allocator size_type

    Fraser Ross, Jul 31, 2003, in forum: C++
    Replies:
    1
    Views:
    435
    John Harrison
    Jul 31, 2003
  3. =?ISO-8859-1?Q?Ralf_Schneewei=DF?=

    How to write an allocator for the STL List in VC++ 6.0

    =?ISO-8859-1?Q?Ralf_Schneewei=DF?=, Aug 20, 2003, in forum: C++
    Replies:
    2
    Views:
    604
    Shane Beasley
    Aug 21, 2003
  4. Alexander Stippler

    allocator traits?

    Alexander Stippler, Sep 2, 2003, in forum: C++
    Replies:
    4
    Views:
    386
    Alexander Stippler
    Sep 3, 2003
  5. Brian Genisio
    Replies:
    12
    Views:
    8,051
    tom_usenet
    Jan 15, 2004
Loading...

Share This Page