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

Home Posts Topics Members FAQ

allocator for map

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.
Oct 3 '05 #1
1 1469

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

Oct 3 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

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;
3
1533
by: Orjan Westin | last post by:
Hi, I have an interesting (read frustrating) problem. I'm writing a generic container class, which holds data as well as links to other instances of itself, like this: template<class T> class node { public:
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>
3
3257
by: Mark P | last post by:
Hi, I'm looking for some info on the default STL allocator, std::alloc. In particular, I'm wondering if it is optimized to handle many allocations of small objects. I'm thinking along the lines of a pool-based approach, for example. Any references would also be appreciated. Thanks,
2
3448
by: Joshua Kolden | last post by:
STL allocators are templates so that when you write one you are obliged to make it work with any type. However, the Intel IPP library that we use has memory aligned allocators for each of 15 different types. For example an 8 bit unsigned array is allocated with ippsMalloc_8u(size). So I want to create memory aligned allocators for use with the STL (in particular the vector container) that is fast (due to alignment), and pointer...
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
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;
2
2480
by: * Tong * | last post by:
Hi, I'm following the example in "C++ Templates: The Complete Guide", section 5.4 Template Template Parameters. It's basics/stack8.hpp example has a statement of "#include <allocator>", but I got an error for it. $ cat -n allocator.cc 1 #include <allocator>
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 ?
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
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...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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();...
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
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.