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

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

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. alex
    Replies:
    1
    Views:
    865
    Lau Lei Cheong
    Feb 4, 2005
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    9,206
    krogers
    Mar 20, 2012
  3. Vlad
    Replies:
    0
    Views:
    527
  4. Patrick Guio
    Replies:
    6
    Views:
    3,551
    chris
    Oct 20, 2004
  5. Erik Arner
    Replies:
    0
    Views:
    1,463
    Erik Arner
    Nov 2, 2004
  6. ma740988

    map within map

    ma740988, Nov 7, 2004, in forum: C++
    Replies:
    0
    Views:
    3,026
    ma740988
    Nov 7, 2004
  7. Noixe

    Help, Map of map

    Noixe, Jan 1, 2005, in forum: C++
    Replies:
    6
    Views:
    567
    =?iso-8859-1?B?Sm9hcXXtbiBNIEzzcGV6IE118W96?=
    Jan 1, 2005
  8. Replies:
    3
    Views:
    1,273
    Howard Hinnant
    Apr 20, 2005
Loading...