How C++ allocators work just like malloc and free

(Last Updated On: May 25, 2010)

How C++ allocators work just like malloc and free
In the C++ standard, the default allocator for objects of type T (cunningly known as allocator) offers the typedefs allocator::pointer and allocator::reference, and it is expected that user-defined allocators will provide these typedefs, too. Let’s be explicit about this: it means that portable allocators may not have any nonstatic data members, at least not any that affect their behavior. None. Nada. That means, for example, you can’t have one SpecialAllocator that allocates from one heap and a different SpecialAllocator that allocates from a different heap. Such allocators wouldn’t be equivalent, and STL implementations exist where attempts to use both allocators could lead to corrupt runtime data structures. the most common forms of operator new and allocator::allocate:
void* operator new(size_t bytes);
pointer allocator::allocate(size_type numObjects);
// recall that “pointer” is a typedef
// that’s virtually always T*
Balanced binary search trees are use associate containers.
template > // implementation
class list{
Allocator alloc; // allocator for objects of type T
struct ListNode{ // nodes in the linked list
T data:
ListNode *prev;
ListNode *next;

When a new node is added to the list, we need to get memory for it from an allocator, but we don’t need memory for a T. we need memory for a ListNode that contains a T. That makes our Allocator object all but useless, because it doesn’t allocate memory for ListNodes, it allocates memory for Ts.
rebind takes a single type parameter, U, and defines nothing but a typedef, other. other is simply a name for A. As a result, list can get from its allocator for T objects (called Allocator) to the corresponding allocator for ListNode objects by referring to Allocator::rebind::other.
Allocator can be slow and a waste of memory. This is how to use an allocator where you have supposed allocators like malloc and free:
void* mallocShared(size_t bytesNeeded);
void freeShared(void *ptr);
class SharedMemoryANocator {

pointer allocate(size_type numObiects, const void *localityHint = 0)
return static_cast(mallocShared(numObiects* sizeof(T)));
void deallocate(pointer ptrToMemory, size_ type numObjects)

You could use SharedMemoryAllocator like this:
// convenience typedef
typedef vector >

{ // begin some block
SharedDoubleVec v: // create a vector whose elements
// are in shared memory
… // end the block
the runtime system uses for all normal stack-based objects. That’s almost never shared memory. To put both v’s contents and v itself into shared memory, you’d have to do something like this:
void *pVectorMemory = // allocate enough shared
mallocShared(sizeof(SharedDoubleVec)); // memory to hold a
// SharedDoubleVec object
SharedDoubleVec *pv = // use “placement new” to
new (pVectorMemory) SharedDoubleVec; // create a SharedDoubleVec
// object in the memory;
// see below
// use the object (via pv)

pv->~SharedDoubleVec(); // destroy the object in the
// shared memory
freeShared(pVectorMemory); // deallocate the initial
// chunk of shared memory

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Subscribe For Latest Updates

Sign up to best of business news, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.


Check NEW site on stock forex and ETF analysis and automation

Scroll to Top