We recommend using Visual Studio 2017

allocator_base Class

 

The latest version of this topic can be found at allocator_base Class.

Defines the base class and common functions needed to create a user-defined allocator from a synchronization filter.

template <class Type, class Sync>  
class allocator_base

Parameters

ParameterDescription
TypeThe type of elements allocated by the allocator.
SyncThe synchronization policy for the allocator, which is sync_none Class, sync_per_container Class, sync_per_thread Class, or sync_shared Class.

Constructors

allocator_baseConstructs an object of type allocator_base.

TypeDefs

const_pointerA type that provides a constant pointer to the type of object managed by the allocator.
const_referenceA type that provides a constant reference to type of object managed by the allocator.
difference_typeA signed integral type that can represent the difference between values of pointers to the type of object managed by the allocator.
pointerA type that provides a pointer to the type of object managed by the allocator.
referenceA type that provides a reference to the type of object managed by the allocator.
size_typeAn unsigned integral type that can represent the length of any sequence that an object of template class allocator_base can allocate.
value_typeA type that is managed by the allocator.

Member Functions

_CharallocAllocates storage for an array of type char.
_ChardeallocFrees storage for the array containing elements of type char.
addressFinds the address of an object whose value is specified.
allocateAllocates a block of memory large enough to store at least some specified number of elements.
constructConstructs a specific type of object at a specified address that is initialized with a specified value.
deallocateFrees a specified number of objects from storage beginning at a specified position.
destroyCalls an objects destructor without deallocating the memory where the object was stored.
max_sizeReturns the number of elements of type Type that could be allocated by an object of class allocator before the free memory is used up.

Header: <allocators>

Namespace: stdext

Allocates storage for an array of type char.

char *_Charalloc(size_type count);

Parameters

ParameterDescription
countThe number of elements in the array to be allocated.

Return Value

A pointer to the allocated object.

Remarks

This member function is used by containers when compiled with a compiler that cannot compile rebind. It implements _Charalloc for the user-defined allocator by returning the result of a call to the allocate function of the synchronization filter.

Frees storage for the array containing elements of type char.

void _Chardealloc(void* ptr, size_type count);

Parameters

ParameterDescription
ptrA pointer to the first object to be deallocated from storage.
countThe number of objects to be deallocated from storage.

Remarks

This member function is used by containers when compiled with a compiler that cannot compile rebind. It implements _Chardealloc for the user-defined allocator by calling the deallocate function of the synchronization filter. The pointer ptr must have been earlier returned by a call to _Charalloc for an allocator object that compares equal to *this, allocating an array object of the same size and type. _Chardealloc never throws an exception.

Finds the address of an object whose value is specified.

pointer address(reference val);

const_pointer address(const_reference val);

Parameters

val
The const or nonconst value of the object whose address is being searched for.

Return Value

A const or nonconst pointer to the object found of, respectively, const or nonconst value.

Remarks

This member function is implemented for the user-defined allocator by returning &val.

Allocates a block of memory large enough to store at least some specified number of elements.

template <class Other>  
pointer allocate(size_type _Nx, const Other* _Hint = 0);

pointer allocate(size_type _Nx);

Parameters

ParameterDescription
_NxThe number of elements in the array to be allocated.
_HintThis parameter is ignored.

Return Value

A pointer to the allocated object.

Remarks

The member function implements memory allocation for the user-defined allocator by returning the result of a call to the allocate function of the synchronization filter of type Type * if _Nx == 1, otherwise by returning the result of a call to operator new(_Nx * sizeof(Type)) cast to type Type *.

Constructs an object of type allocator_base.

allocator_base();

template <class Other>  
allocator_base(const allocator_base<Other, Sync>& right);

Parameters

ParameterDescription
rightThe allocator object to be copied.

Remarks

The first constructor constructs an allocator_base instance. The second constructor constructs an allocator_base instance such that for any allocator_base<Type, _Sync> instance a, allocator_base<Type, Sync>(allocator_base<Other, Sync>(a)) == a.

A type that provides a constant pointer to the type of object managed by the allocator.

typedef const Type *const_pointer;

A type that provides a constant reference to type of object managed by the allocator.

typedef const Type& const_reference;

Constructs a specific type of object at a specified address that is initialized with a specified value.

void construct(pointer ptr, const Type& val);

Parameters

ParameterDescription
ptrA pointer to the location where the object is to be constructed.
valThe value with which the object being constructed is to be initialized.

Remarks

This member function is implemented for the user-defined allocator by calling new((void*)ptr Type(val).

Frees a specified number of objects from storage beginning at a specified position.

void deallocate(pointer ptr, size_type _Nx);

Parameters

ParameterDescription
ptrA pointer to the first object to be deallocated from storage.
_NxThe number of objects to be deallocated from storage.

Remarks

This member function is implemented for the user-defined allocator by calling deallocate(ptr) on the synchronization filter Sync if _Nx == 1, otherwise by calling operator delete(_Nx * ptr).

Calls an objects destructor without deallocating the memory where the object was stored.

void destroy(pointer ptr);

Parameters

ParameterDescription
ptrA pointer designating the address of the object to be destroyed.

Remarks

This member function is implemented for the user-defined allocator by calling ptr->~Type().

A signed integral type that can represent the difference between values of pointers to the type of object managed by the allocator.

typedef std::ptrdiff_t difference_type;

Returns the number of elements of type Type that could be allocated by an object of class allocator before the free memory is used up.

size_type max_size() const;

Return Value

The number of elements that could be allocated.

Remarks

This member function is implemented for the user-defined allocator by returning (size_t)-1 / sizeof(Type) if 0 < (size_t)-1 / sizeof(Type), otherwise 1.

A type that provides a pointer to the type of object managed by the allocator.

typedef Type *pointer;

A type that provides a reference to the type of object managed by the allocator.

typedef Type& reference;

An unsigned integral type that can represent the length of any sequence that an object of template class allocator_base can allocate.

typedef std::size_t size_type;

A type that is managed by the allocator.

typedef Type value_type;

<allocators>

Show: