std::free(3)
β¨ NAME β¨
std::free - std::free. Yep bestie, that's the NAME line β it's literally the same thing twice. This is the std::free(3) page from the C++ Standard Library (the header spells it C++ Standard Libary, and I'm not correcting it; we respect the typo π).
π¦ Synopsis π¦
Defined in header <cstdlib>:
Defined in header <cstdlib>
void free( void* ptr );
Deallocates the space previously allocated by std::malloc, std::calloc, std::aligned_alloc (since C++17), or std::realloc. That's the whole assignment. Cleanup era π
.
If ptr is a null pointer, the function does nothing. Null? Cute. No work today.
π¨ The behavior is undefined if the value ofptrdoes not equal a value returned earlier bystd::malloc,std::calloc,std::aligned_alloc(since C++17), orstd::realloc.
π¨ The behavior is undefined if the memory area referred to byptrhas already been deallocated, that is,std::freeorstd::reallochas already been called withptras the argument and no calls tostd::malloc,std::calloc,std::aligned_alloc(since C++17), orstd::reallocresulted in a pointer equal toptrafterwards.
π¨ The behavior is undefined if afterstd::freereturns, an access is made through the pointerptr(unless another allocation function happened to result in a pointer value equal toptr).
The following functions are required to be thread-safe:
- The library versions of
operator newandoperator delete - User replacement versions of global
operator newandoperator delete std::calloc,std::malloc,std::realloc(since C++11),std::aligned_alloc(since C++17),std::free
Calls to these functions that allocate or deallocate a particular unit of storage occur in a single total order, and each such deallocation call happens-before the next allocation (if any) in this order. No cap, that's the ordering tea β.
πΈ Parameters πΈ
ptrβ pointer to the memory to deallocate.
π Return value π
(none) β the section literally says (none). No return value, no drama, just vibes.
π Notes π
The function accepts (and does nothing with) the null pointer to reduce the amount of special-casing. Whether allocation succeeds or not, the pointer returned by an allocation function can be passed to std::free. So yes, the null pointer is invited to the function and gets to chill π«Ά.
π₯ Example π₯
Run this code, bestie:
// Run this code
#include <cstdlib>
int main()
{
int* p1 = (int*)std::malloc(10 * sizeof *p1);
std::free(p1); // every allocated pointer must be freed
int* p2 = (int*)std::calloc(10, sizeof *p2);
int* p3 = (int*)std::realloc(p2, 1000 * sizeof *p3);
if (!p3) // p3 null means realloc failed and p2 must be freed.
std::free(p2);
std::free(p3); // p3 can be freed whether or not it is null.
}
π See also π
- C documentation for
free
source: http://cppreference.com β’ version/date: 2024.06.10 β’ page: std::free(3) β bye bestie, go free your pointers (the valid ones, okay?) πβ¨
π¦ 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