C++ allocator class member functions

Here we will see the C++ allocator class member functions. The meber functions are listed below.

Link :C++11 allocator class


There are two non-member functions.
i)operator==() and

All the member functions are discussed below.


This function will accept a value/variable as an argument and return the address of that value. The address obtain is same as the address value obtained by using the ‘&‘ operator. So you can even pass a random variable not belonging to any array element allocated by the allocator object.

allocator<int> al ;

int *mem=al.allocate(2) ;

mem[0]=90 ;
mem[1]=404 ;

cout<< al.allocate(*mem) << endl
  << mem ;

in ival=890;

cout<< al.address(ival) << endl //Passing random variable
  << &ival ; //output is the same as above

al.deallocate(mem , 2) ; //do not forget!


This function returns the maximum size the allocator can allocate.


cout<< alSt.max_size() << endl ;

allocate( )

This function will allocate memory in the heap. The amount of memory allocated is “n * sizeof(type)“, where ‘n’ is the argument passed and it denotes the number of array elements. Note if the type is class it does not call the constructor, it only allocates the memory.

class A {};

allocator<A> alA;

A *a=alA.allocate(2); ///does not call the default constructor

Here since the constructor is not called initialization is not performed ,it is performed by calling the function construct().

construct( )

If the type is built-in this function will store the value in the memory mentioned as the first argument. And if the type is class the constructor is called to perform the initialization. If any constructor is not provided it calls the default constructor.

class A { };

allocator<A> alA;

A *a=alA.allocate(2);

alA.construct(a , A) ;//default constructor is called
alA.construct(a+1 ,A) ;//default constructor is called


destroy( )

If the type is a class then this function will call the destructor of the class.For built-in type this function does nothing.

A *a=alA.allocate(2); //does not call the default constructor

alA.construct(a , A()); //default constructor is called
alA.construct(a+1 , A(90)); //default constructor is called

alA.destory(a) ; //default destructor called
alA.destory(a+1) ; //default destructor called

cout<< a[0].


Calling destory() for built-in type

allocator<double> alDb ;
double *dbMem=alDb.allocate(2) ;

alDb.allocate(dbMem, 9.098) ;
alDb.allocate(dbMem ,789 ) ;

alDb.destroy(dbMem );
alDb.destroy(dbMem+1) ;

cout<< dbMem[0] << endl //print 9.098 
<< dbMem[1] ; ///print 789

dbMem[0]=980.8 ;
dbMem[1]=.048 ;

abMem.deallocate(dbMem , 2);



This function will free the storage allocated in the heap. The allocate() function does not allocate storage using the array new operator. It uses the normal new operator to allocate the storage and return the pointer to that memory.

In our programs the address is usually assigned to the raw pointer. Using the raw pointer and the delete operator we can also free the storage without actually calling the deallocate() function.

allocator<double> alDb;

double *dbMem=alDb.allocate(2);

alDb.deallocate( dbMem ,2 ) ; //storage deleted

//::operator delete(dbMem) ; //also work fine

The storage will be deleted safely by also calling the ::operator delete().

operator==() and operator!=() functions

As stated earlier the objects of allocator class of similar type are the same and the objects of different types are interchangeable. So the function operator==() will always yield true and the function operator!=() will always yield false.

allocator<int> iAl , iAl1 ;

allocator<string> sAl , sAl1 ;

allocator<A> alA ;

cout<< (iAl==iAl1) << endl //give true
  << (alA==iAl) ; //give true

cout<< (iAl1!=aAl) << endl //give false
<< ( alA!=sAl1 ) ; //give false