473,803 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

writing allocator for STL maps

12 New Member
Hi,
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???
-Thanks
-Mavrik
Mar 16 '08 #1
7 11431
Banfa
9,065 Recognized Expert Moderator Expert
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
mavrik
12 New Member
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??
-Thanks
Mar 17 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
The allocator is a defaulted template argument. You should be able to create your map by overriding this default.
Mar 17 '08 #4
mavrik
12 New Member
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.

-Thanks
Mar 17 '08 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
The map template looks like:
template <
class Key,
class Type,
class Traits = less<Key>,
class Allocator=alloc ator<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
mavrik
12 New Member
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<pai r<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...
Thanks
Mar 20 '08 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
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. }    
  6.  
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.         };
  17.  
  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);
  32.  
  33.   };
  34.  
  35.   // globals
  36.   template <class T, class U>
  37.   bool operator==(const myAllocator<T>&, 
  38.                   const myAllocator<U>&) throw();
  39.  
  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. }    
  48.  
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

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

Similar topics

5
2106
by: Scott Brady Drummonds | last post by:
Hi, everyone, A coworker and I have been pondering a memory allocation problem that we're having with a very large process. Our joint research has led us to the conclusion that we may have to replace the STL allocator. But, before I even attempt something like this, I wanted to ask some questions to this group: 1) We read online that STL caches allocated memory so that containers and their contents that are freed at one point in...
3
1880
by: Bernhard Kick | last post by:
Hi all, I saw this code in the book "Accelerated C++" (chapt 11, iirc): template <class T> class Vec { ... std::allocator<T> alloc; // object to handle memory allocation // ??would static member work here??: // static std::allocator<T> alloc;
13
1952
by: John Harrison | last post by:
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type, or can you assume that the container will rebind the allocator to the correct type? For example I tried the following code which uses a 'wrong' allocator and was slightly surrised to find it compiles on the three compilers I tried it on #include <vector> #include <memory>
7
3081
by: Grahamo | last post by:
Hi, can anybody tell me where I can get the boiler plate code for std::allocator. I need to have my version of new and delete called and want to get reference code. My compilers headers are all over the place with #ifdefs and what not, I'd like a clean copy. thanks much
3
2784
by: Protoman | last post by:
Why won't the compiler let me write this: template <class T> class Allocator { public: Allocator(){} T* allocate(){static long long i=0; T* j=new(pool) T; i++; return &*j;} private:
6
2754
by: mavrik | last post by:
Hi All, I've created STL allocator which creates memory pool in the beginning, and all maps and vectors will take memory from that memory pool. But...I am observing that application has become drastically slow. I was under impression that after implemeting pool, application will fasten, but it is doing other ways. I've implemeted doubly linked list to maintain the nodes occupied or free in the pool. My C++ application was able to...
6
13861
by: Juha Nieminen | last post by:
I tested the speed of a simple program like this: //------------------------------------------------------------ #include <list> #include <boost/pool/pool_alloc.hpp> int main() { typedef std::list<intList_t; // default allocator //typedef std::list<int, boost::pool_allocator<int List_t;
16
2706
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
12
3007
by: David Given | last post by:
I have a situation where I need to be able to allocate chunks on unmapped virtual memory. Because the memory I'm allocating isn't mapped, I can't use a normal memory allocator, as most of them want to store their metadata about the free list in the unused holes in the heap. Instead, I need a memory allocator that stores its metadata out-of-line (perversely enough, a simple malloc() heap would be fine for that). Does anyone know of...
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.