allocator for map

F

Fred Zwarts

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

mlimber

Fred said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top