Allocators in C++

We explain in the following about allocators in c++.Encapsulates a memory allocation and deallocation strategy. Every standard library component that may need to allocate or release storage, from std::string,std::vector,and every container except std::array,to std::shared_ptr and std::function, does so through an Allocator: an object of a class type that satisfies given following requirements.

Some requirements are optional: the template std::allocator_traits supplies the default implementations for all optional requirements, and all standard library containers and other allocator-aware classes access the allocator through std::allocator_traits not directly.

GIVEN REQUIREMENTS :
A, an Allocator type for type T

a, an object of type A

B, the corresponding Allocator type for type U (as obtained by rebinding A)

ptr, a value of type allocator_traits<A>::pointer, obtained by calling allocator_traits<A>::allocate()

cptr, a value of type allocator_traits<A>::const_pointer, obtained by conversion from ptr

vptr, a value of type allocator_traits<A>::void_pointer, obtained by conversion from ptr

cvptr, a value of type allocator_traits<A>::const_void_pointer, obtained by conversion from cptr or from vptr

xptr, a dereferencable pointer to some type X

 

ExpressionRequirementsReturn type
A::pointer (optional)Satisfies Nullablepointer and RandomAccessIterator
A::const_pointer (optional)A::pointer is convertible to A::const_pointer. Satisfies Nullablepointer and RandomAccessIterator
A::void_pointer (optional)A::pointer is convertible to A::void_pointerB::void_pointer and A::void_pointer are the same type. Satisfies Nullablepointer
A::const_void_pointer (optional)A::pointer, A::const_pointer, and A::void_pointer are convertible to A::const_void_pointerB::const_void_pointer and A::const_void_pointer are the same type. Satisfies Nullablepointer
A::value_typethe type T
A::size_type (optional)A::size_type can represent the size of the largest object A can allocateunsigned integer type
A::difference_type (optional)A::difference_type can represent the difference of any two pointers to the objects allocated by Asigned integer type
A::template rebind<U>::other (optional[1])for any U, B::template rebind<T>::other is Athe type B
*ptrT&
*cptr*cptr and *ptr identify the same objectconst T&
ptr->msame as (*ptr).m, if (*ptr).m is well-definedthe type of T::m
cptr->msame as (*cptr).m, if (*cptr).m is well-definedthe type of T::m
static_cast<A::pointer>(vptr)static_cast<A::pointer>(vptr) == ptrA::pointer
static_cast<A::const_pointer>(cvptr)static_cast<A::const_pointer>(vptr) == cptrA::const_pointer
a.allocate(n)allocates storage suitable for n objects of type T, but does not construct them. May throw exceptions.A::pointer
a.allocate(n, cvptr) (optional)same as a.allocate(n), but may use cvptr (a pointer obtained from a.allocate() or nullptr_t) in unspecified manner to aid localityA::pointer
a.deallocate(ptr, n)deallocates storage previously allocated by a call to a.allocate(n). Does not call destructors, if any objects were constructed, they must be destroyed before calling a.deallocate(). Does not throw exceptions.(not used)
a.max_size() (optional)the largest value that can be passed to A::allocate()A::size_type
a1 == a2returns true only if the storage allocated by the allocator a1 can be deallocated through a2. Establishes reflexive, symmetric, and transitive relationship. Does not throw exceptions.bool
a1 != a2same as !(a1==a2)bool
A a1(a)A a1 = aCopy-constructs a1 such that a1 == a. Does not throw exceptions. (Note: every Allocator also satisfies CopyConstructible)
A a(b)Constructs a such that B(a)==b and A(b)==a. Does not throw exceptions. (Note: this implies that all allocators related by rebind maintain each other’s resources, such as memory pools)
A a1(std::move(a))A a1 = std::move(a)Constructs a1 such that it equals the prior value of a. Does not throw exceptions.
A a(std::move(b))Constructs a such that it equals the prior value of A(b). Does not throw exceptions.
a.construct(xptr, args) (optional)Constructs an object of type X in previously-allocated storage at the address pointed to by xptr, using args as the constructor arguments
a.destroy(xptr) (optional)Destructs an object of type X pointed to by xptr, but does not deallocate any storage.
a.select_on_container_copy_construction() (optional)Provides an instance of A to be used by the container that is copy-constructed from the one that uses a currently. Usually returns either a copy of a or a default-constructed A().A
A::propagate_on_container_copy_assignment (optional)true if the allocator of type A needs to be copied when the container that uses it is copy-assigned. Note that if the allocators of the source and the target containers do not compare equal, copy assignment has to deallocate the target’s memory using the old allocator and then allocate it using the new allocator before copying the elements (and the allocator).std::true_type or std::false_type or derived from such
A::propagate_on_container_move_assignment (optional)true if the allocator of type A needs to be copied when the container that uses it is move-assigned. If this member is false and the allocators of the source and the target containers do not compare equal, move assignment cannot take ownership of the source memory and must move-assign or move-construct the elements individually, resizing its own memory as needed.std::true_type or std::false_type or derived from such
A::propagate_on_container_swap (optional)true if the allocators of type A need to be swapped when two containers that use them are swapped. If this member is false and the allocators of the two containers do not compare equal, the behavior of container swap is undefined.std::true_type or std::false_type or derived from such
A::is_always_equal (since c++17) (optional)true if any two allocators of type A always compare equal. If not provided, std::allocator_traits defaults this to equal std::is_empty<A>::typestd::true_type or std::false_type or derived from such

 

More in sem3
dams and their effects on forests and tribal people

dams and their effects on forests and tribal people When asked to name different causes of deforestation, few people will...

Close