Name
allocator class template — Encapsulates memory allocation and deallocation
Synopsis
template <class T> class allocator { public: typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; template <class U> struct rebind { typedef allocator<U> other; }; allocator( ) throw( ); allocator(const allocator&) throw( ); template <class U> allocator(const allocator<U>&) throw( ); ~allocator( ) throw( ); pointer address(reference x) const; const_pointer address(const_reference x) const; pointer allocate(size_type, allocator<void>::const_pointer hint = 0); void deallocate(pointer p, size_type n); size_type max_size( ) const throw( ); void construct(pointer p, const T& val); void destroy(pointer p); };
The allocator
class
template encapsulates basic allocation and deallocation functions.
The standard containers rely on allocators for memory management and
use allocator
as the default
allocator.
Most programmers do not need to use allocator
, which offers few advantages
over plain new
and delete
. However, if you want to write your
own container, or provide a custom allocator for the standard
containers, you should take the time to understand allocator
.
Perhaps the easiest way to understand allocator
is to take a look at a trivial
implementation in Example
13-30. Note that a library might have a more complicated implementation to handle multithreading, improve performance, ...
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.