std::allocator
From cppreference.com
< cpp | memory
C++
Language | ||||
Standard library headers | ||||
Concepts | ||||
Utilities library | ||||
Strings library | ||||
Containers library | ||||
Algorithms library | ||||
Iterators library | ||||
Numerics library | ||||
Input/output library | ||||
Localizations library | ||||
Regular expressions library (C++11) | ||||
Atomic operations library (C++11) | ||||
Thread support library (C++11) | ||||
Technical Specifications |
[edit]
Dynamic memory management
Smart pointers | |||||||||||||||||||||
unique_ptr (C++11) | |||||||||||||||||||||
shared_ptr (C++11) | |||||||||||||||||||||
weak_ptr (C++11) | |||||||||||||||||||||
auto_ptr (until C++17) | |||||||||||||||||||||
owner_less (C++11) | |||||||||||||||||||||
enable_shared_from_this (C++11) | |||||||||||||||||||||
bad_weak_ptr (C++11) | |||||||||||||||||||||
default_delete (C++11) | |||||||||||||||||||||
Allocators | |||||||||||||||||||||
allocator | |||||||||||||||||||||
allocator_traits (C++11) | |||||||||||||||||||||
allocator_arg_t (C++11) | |||||||||||||||||||||
allocator_arg (C++11) | |||||||||||||||||||||
uses_allocator (C++11) | |||||||||||||||||||||
scoped_allocator_adaptor (C++11) | |||||||||||||||||||||
Uninitialized storage | |||||||||||||||||||||
uninitialized_copy | |||||||||||||||||||||
uninitialized_copy_n (C++11) | |||||||||||||||||||||
uninitialized_fill | |||||||||||||||||||||
uninitialized_fill_n | |||||||||||||||||||||
raw_storage_iterator | |||||||||||||||||||||
get_temporary_buffer | |||||||||||||||||||||
return_temporary_buffer | |||||||||||||||||||||
Garbage collection support | |||||||||||||||||||||
declare_reachable (C++11) | |||||||||||||||||||||
undeclare_reachable (C++11) | |||||||||||||||||||||
declare_no_pointers (C++11) | |||||||||||||||||||||
undeclare_no_pointers (C++11) | |||||||||||||||||||||
pointer_safety (C++11) | |||||||||||||||||||||
get_pointer_safety (C++11) | |||||||||||||||||||||
Miscellaneous | |||||||||||||||||||||
pointer_traits (C++11) | |||||||||||||||||||||
addressof (C++11) | |||||||||||||||||||||
align (C++11) | |||||||||||||||||||||
C Library | |||||||||||||||||||||
|
| ||||||||||||||||||||
Low level memory management |
[edit]
std::allocator
Member functions | ||||
allocator::allocator | ||||
allocator::~allocator | ||||
allocator::address | ||||
allocator::allocate | ||||
allocator::deallocate | ||||
allocator::max_size | ||||
allocator::construct | ||||
allocator::destroy | ||||
Non-member functions | ||||
operator==operator!= |
[edit]
Defined in header <memory>
|
||
template< class T > struct allocator; |
(1) | |
template<> struct allocator<void>; |
(2) | |
The std::allocator
class template is the default Allocator
used by all standard library containers if no user-specified allocator is provided. The default allocator is stateless, that is, all instances of the given allocator are interchangeable, compare equal and can deallocate memory allocated by any other instance of the same allocator type.
Specialization for void lacks the member typedefs reference
, const_reference
, size_type
and difference_type
. This specialization declares no member functions.
All custom allocators also must be stateless. | (until C++11) |
Custom allocators may contain state. Each container or another allocator-aware object stores an instance of the supplied allocator and controls allocator replacement through std::allocator_traits. | (since C++11) |
The default allocator satisfies allocator completeness requirements. | (since C++17) |
Contents
|
[edit] Member types
Type | Definition |
value_type
|
T |
pointer
|
T* |
const_pointer
|
const T* |
reference
|
T& |
const_reference
|
const T& |
size_type
|
std::size_t |
difference_type
|
std::ptrdiff_t |
propagate_on_container_move_assignment (C++14)
|
std::true_type |
rebind
|
template< class U > struct rebind { typedef allocator<U> other; }; |
is_always_equal (C++17)
|
std::true_type |
[edit] Member functions
(constructor) |
creates a new allocator instance (public member function) [edit] |
(destructor) |
destructs an allocator instance (public member function) [edit] |
address |
obtains the address of an object, even if operator& is overloaded (public member function) [edit] |
allocate |
allocates uninitialized storage (public member function) [edit] |
deallocate |
deallocates storage (public member function) [edit] |
max_size |
returns the largest supported allocation size (public member function) [edit] |
construct |
constructs an object in allocated storage (public member function) [edit] |
destroy |
destructs an object in allocated storage (public member function) [edit] |
[edit] Non-member functions
operator==operator!= |
compares two allocator instances (public member function) [edit] |
[edit] Notes
The member template class rebind
provides a way to obtain an allocator for a different type. For example,
std::list<T, A> allocates nodes of some internal type Node<T> , using the allocator A::rebind<Node<T>>::other
|
(until C++11) |
std::list<T, A> allocates nodes of some internal type Node<T> , using the allocator std::allocator_traits<A>::rebind_alloc<Node<T>>, which is implemented in terms of A::rebind<Node<T>>::other if A is an std::allocator
|
(since C++11) |
[edit] Example
Run this code
#include <memory> #include <iostream> #include <string> int main() { std::allocator<int> a1; // default allocator for ints int* a = a1.allocate(10); // space for 10 ints a[9] = 7; std::cout << a[9] << '\n'; a1.deallocate(a, 10); // default allocator for strings std::allocator<std::string> a2; // same, but obtained by rebinding from the type of a1 decltype(a1)::rebind<std::string>::other a2_1; // same, but obtained by rebinding from the type of a1 via allocator_traits std::allocator_traits<decltype(a1)>::rebind_alloc<std::string> a2_2; std::string* s = a2.allocate(2); // space for 2 strings a2.construct(s, "foo"); a2.construct(s + 1, "bar"); std::cout << s[0] << ' ' << s[1] << '\n'; a2.destroy(s); a2.destroy(s + 1); a2.deallocate(s, 2); }
Output:
7 foo bar
[edit] See also
allocator_traits (C++11) |
provides information about allocator types (class template) |