470,863 Members | 1,609 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,863 developers. It's quick & easy.

writing allocator for STL maps

I've written allocator for STL vectors, which take all memory from my heap.
But I don't have any idea to implement the same for Maps.

As maps take Key and Values. So should I use the same allocator for both data types, or the allocator for map should ahve different signature???
Mar 16 '08 #1
7 11192
9,065 Expert Mod 8TB
Erm, I don't think this should be necessary, by default the STL containers allocate their memory from the program heap.
Mar 17 '08 #2
I know that default allocators take memory from heap.
But my requirement is to create a pool of memory and all allocations of memory should be done from that memory pool.
I've created memory pool for all type of data types, and I am using that memory for all allocations of vector, as I've overriden new and delete in allocator of vector.
Now I want to do the same for maps.
So I am not sure how to do that??
Any Idea??
Mar 17 '08 #3
9,208 Expert Mod 8TB
The allocator is a defaulted template argument. You should be able to create your map by overriding this default.
Mar 17 '08 #4
Maps take key and value as pair type.
So how should I allocate memory for both types(kwy and value ) from my allocator which takes just one typename as template.

Mar 17 '08 #5
9,208 Expert Mod 8TB
The map template looks like:
template <
class Key,
class Type,
class Traits = less<Key>,
class Allocator=allocator<pair <const Key, Type> >
class map
There are actually four template parameters. The first two are for your pair. The third is the ordering function pointer (which you cna specify if you don't like the default) and the fourth is your allocator.
Mar 18 '08 #6
Thank u all for ur replies...
I still have one more query..

I am using my allocator for vectors as follows.
vector<int myAllocator<int>> v1;

now to use the same allocaor for maps, I should do as follows...
map<int , long , less<int> , myAllocator<pair<int,long> > >,
as pair is a tyoe now..
Is that right???
If yes... then allocate() function of my allocator does not recognise pair as a data type. Rather it considers it as map.
And allocate memory for sizeof(map), not actually sizeof(int) + sizeof(long);
If I am clear in communicating my problem, can you suggest something??
It has become a real problem for me now...
Mar 20 '08 #7
9,208 Expert Mod 8TB
It should work this way:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     map<int , long , less<int> , myAllocator<pair<int, long> > > myMap;
  4.     vector<int, myAllocator<int> > myVector;
  5. }    
Your allocator is to allocate memory for the type you need. In the case of the map you need memory for a pair. In the case of the vector, you need memory for an int.

What I did was take the standard allocator and modify it to myAllocator sso the above code would compile. Here is what I did:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2.   class myAllocator
  3.   {
  4.     public:
  5.         typedef size_t            size_type;
  6.         typedef ptrdiff_t         difference_type;
  7.         typedef T*                pointer;
  8.         typedef const T*          const_pointer;
  9.         typedef T&                reference;
  10.         typedef const T&          const_reference;
  11.         typedef T                 value_type;      
  12.         template <class U>    
  13.         struct rebind
  14.         {
  15.           typedef myAllocator<U> other;
  16.         };
  18.    myAllocator () throw();
  19.    myAllocator (const myAllocator&) throw ();
  20.     template <class U> 
  21.     myAllocator(const myAllocator<U>&) throw();
  22.    template <class U> 
  23.    myAllocator& operator=(const myAllocator<U>&) throw();
  24.     ~myAllocator () throw();
  25.     pointer address(reference) const;
  26.     const_pointer address(const_reference) const;
  27.     pointer allocate (size_type, typename myAllocator<T>::const_pointer = 0);
  28.     void deallocate(pointer p, size_type n); 
  29.     size_type max_size() const throw();
  30.     void construct(pointer, const T&);
  31.     void destroy(pointer);
  33.   };
  35.   // globals
  36.   template <class T, class U>
  37.   bool operator==(const myAllocator<T>&, 
  38.                   const myAllocator<U>&) throw();
  40.   template <class T, class U>
  41.   bool operator!=(const myAllocator<T>&, 
  42.                   const myAllocator<U>&) throw();
  43.  int main()
  44. {
  45.     map<int , long , less<int> , myAllocator<pair<int, long> > > myMap;
  46.     vector<int, myAllocator<int> > myVector;
  47. }    
This compile fine but dies in the link since the methods aren't written.

Allocators have rules and you can get a peek at them by getting a book Effective STL by Scott Meyers and reading items 10 and 11.

One essential on the allocate method is to be sure the arugment is for the number of objects requiring memory and not the number of bytes.
Mar 20 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Scott Brady Drummonds | last post: by
3 posts views Thread by Bernhard Kick | last post: by
13 posts views Thread by John Harrison | last post: by
7 posts views Thread by Grahamo | last post: by
3 posts views Thread by Protoman | last post: by
6 posts views Thread by Juha Nieminen | last post: by
12 posts views Thread by David Given | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.