std::allocator<T>::deallocate
From cppreference.com
void deallocate( T* p, std::size_t n ); |
(until C++20) | |
constexpr void deallocate( T* p, std::size_t n ); |
(since C++20) | |
Deallocates the storage referenced by the pointer p
, which must be a pointer obtained by an earlier call to allocate().
The argument n
must be equal to the first argument of the call to allocate() that originally produced p
; otherwise, the behavior is undefined.
Calls ::operator delete(void*) or ::operator delete(void*, std::align_val_t) (since C++17), but it is unspecified when and how it is called.
In evaluation of a constant expression, this function must deallocate storage allocated within the evaluation of the same expression. |
(since C++20) |
Contents |
[edit] Parameters
p | - | pointer obtained from allocate()
|
n | - | number of objects earlier passed to allocate()
|
[edit] Return value
(none)
[edit] Example
Run this code
#include <algorithm> #include <iostream> #include <memory> class S { inline static int n{1}; int m{}; void pre() const { std::cout << "#" << m << ' '; } public: S(int x) : m{n++} { pre(); std::cout << "S::S(" << x << ");\n"; } ~S() { pre(); std::cout << "S::~S();\n"; } void id() const { pre(); std::cout << "S::id();\n"; } }; int main() { constexpr std::size_t n{4}; std::allocator<S> allocator; S* s = allocator.allocate(n); for (std::size_t i = 0; i != n; ++i) { // allocator.construct(&s[i], i+2); // removed in C++20 std::construct_at(&s[i], i+2); // since C++20 } std::for_each(s, s + n, [](const auto& e) { e.id(); }); std::destroy_n(s, n); allocator.deallocate(s, n); }
Output:
#1 S::S(2); #2 S::S(3); #3 S::S(4); #4 S::S(5); #1 S::id(); #2 S::id(); #3 S::id(); #4 S::id(); #1 S::~S(); #2 S::~S(); #3 S::~S(); #4 S::~S();
[edit] See also
allocates uninitialized storage (public member function) | |
[static] |
deallocates storage using the allocator (public static member function of std::allocator_traits<Alloc> )
|