std::allocator::deallocate(3)
β¨ hiii bestie β¨ welcome to the std::allocator::deallocate(3) page from the C++ Standard Libary manual. Yes, the header spells it βLibaryβ β weβre keeping the vibes immaculate and the facts untouched. π
π NAME π
std::allocator::deallocateβ literallystd::allocator::deallocate, no cap. π«Ά
π Synopsis π
void deallocate( T* p, std::size_t n ); (constexpr since C++20)
Okay so this one deallocates the storage referenced by the pointer p, and p must be a pointer obtained by an earlier call to allocate() or allocate_at_least() (since C++23). Like, it has to come from there, bestie. No random pointers. π
The argument n must be equal to the first argument of the call to allocate() that originally produced p, or in the range [m, count] if p is obtained from a call to allocate_at_least(m) which returned {p, count} (since C++23); otherwise, the behavior is undefined. UNDEFINED, bestie β thatβs the scary one. π
It 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. So likeβ¦ it happens, but the details are mysterious. β¨
In evaluation of a constant expression, this function must deallocate (since C++20) storage allocated within the evaluation of the same expression. π§
π§Έ Parameters π§Έ
pβ pointer obtained fromallocate()orallocate_at_least()(since C++23). πnβ number of objects earlier passed toallocate(), or a number between requested and actually allocated number of objects viaallocate_at_least()(may be equal to either bound) (since C++23). πΈ
π Return value π
(none)
Nada. Nothing. Itβs giving βmy job here is doneβ energy. π
π» Example π»
// Run this code
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <memory>
#include <string>
class S
{
inline static int n{1};
int m{};
void pre() const { std::cout << "#" << m << std::string(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;
try
{
S* s = allocator.allocate(n); // may throw
for (std::size_t i{}; i != n; ++i)
{
// allocator.construct(&s[i], i + 42); // removed in C++20
std::construct_at(&s[i], i + 42); // since C++20
}
std::for_each_n(s, n, [](const auto& e) { e.id(); });
std::destroy_n(s, n);
allocator.deallocate(s, n);
}
catch (std::bad_array_new_length const& ex) { std::cout <<
ex.what() << '\n'; }
catch (std::bad_alloc const& ex) { std::cout << ex.what() << '\n';
}
}
π₯οΈ Output π₯οΈ
#1 S::S(42);
#2 S::S(43);
#3 S::S(44);
#4 S::S(45);
#1 S::id();
#2 S::id();
#3 S::id();
#4 S::id();
#1 S::~S();
#2 S::~S();
#3 S::~S();
#4 S::~S();
π See also π
allocateβ allocates uninitialized storage; public member function. π¦allocate_at_leastβ allocates uninitialized storage at least as large as requestedsize;(C++23); public member function. βdeallocateβ deallocates storage using the allocator;[static]; public static member function ofstd::allocator_traits<Alloc>. π
Thatβs the tea β β page source: http://cppreference.com, date/version: 2024.06.10, and the page id is std::allocator::deallocate(3). Bye bestie, go free that storage responsibly! πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms