|
|
|
|
Functions | ||||
operator deleteoperator delete[] | ||||
(C++11) | ||||
Classes | ||||
(C++11) | ||||
(C++17) | ||||
Types | ||||
Objects | ||||
(C++20) | ||||
Object access | ||||
(C++17) |
Defined in header <new> | ||
Replaceable usual deallocation functions | ||
(1) | ||
void operator delete (void* ptr)throw(); | (until C++11) | |
void operator delete (void* ptr)noexcept; | (since C++11) | |
(2) | ||
void operator delete[](void* ptr)throw(); | (until C++11) | |
void operator delete[](void* ptr)noexcept; | (since C++11) | |
void operator delete (void* ptr,std::align_val_t al)noexcept; | (3) | (since C++17) |
void operator delete[](void* ptr,std::align_val_t al)noexcept; | (4) | (since C++17) |
void operator delete (void* ptr,std::size_t sz)noexcept; | (5) | (since C++14) |
void operator delete[](void* ptr,std::size_t sz)noexcept; | (6) | (since C++14) |
void operator delete (void* ptr,std::size_t sz, std::align_val_t al)noexcept; | (7) | (since C++17) |
void operator delete[](void* ptr,std::size_t sz, std::align_val_t al)noexcept; | (8) | (since C++17) |
Replaceable placement deallocation functions | ||
(9) | ||
void operator delete (void* ptr,conststd::nothrow_t& tag)throw(); | (until C++11) | |
void operator delete (void* ptr,conststd::nothrow_t& tag)noexcept; | (since C++11) | |
(10) | ||
void operator delete[](void* ptr,conststd::nothrow_t& tag)throw(); | (until C++11) | |
void operator delete[](void* ptr,conststd::nothrow_t& tag)noexcept; | (since C++11) | |
void operator delete (void* ptr,std::align_val_t al, conststd::nothrow_t& tag)noexcept; | (11) | (since C++17) |
void operator delete[](void* ptr,std::align_val_t al, conststd::nothrow_t& tag)noexcept; | (12) | (since C++17) |
Non-allocating placement deallocation functions | ||
(13) | ||
void operator delete (void* ptr,void* place)throw(); | (until C++11) | |
void operator delete (void* ptr,void* place)noexcept; | (since C++11) | |
(14) | ||
void operator delete[](void* ptr,void* place)throw(); | (until C++11) | |
void operator delete[](void* ptr,void* place)noexcept; | (since C++11) | |
User-defined placement deallocation functions | ||
void operator delete (void* ptr, args...); | (15) | |
void operator delete[](void* ptr, args...); | (16) | |
Class-specific usual deallocation functions | ||
void T::operator delete (void* ptr); | (17) | |
void T::operator delete[](void* ptr); | (18) | |
void T::operator delete (void* ptr,std::align_val_t al); | (19) | (since C++17) |
void T::operator delete[](void* ptr,std::align_val_t al); | (20) | (since C++17) |
void T::operator delete (void* ptr,std::size_t sz); | (21) | |
void T::operator delete[](void* ptr,std::size_t sz); | (22) | |
void T::operator delete (void* ptr,std::size_t sz,std::align_val_t al); | (23) | (since C++17) |
void T::operator delete[](void* ptr,std::size_t sz,std::align_val_t al); | (24) | (since C++17) |
Class-specific placement deallocation functions | ||
void T::operator delete (void* ptr, args...); | (25) | |
void T::operator delete[](void* ptr, args...); | (26) | |
Class-specific usual destroying deallocation functions | ||
void T::operator delete( T* ptr,std::destroying_delete_t); | (27) | (since C++20) |
void T::operator delete( T* ptr,std::destroying_delete_t, std::align_val_t al); | (28) | (since C++20) |
void T::operator delete( T* ptr,std::destroying_delete_t,std::size_t sz); | (29) | (since C++20) |
void T::operator delete( T* ptr,std::destroying_delete_t, std::size_t sz,std::align_val_t al); | (30) | (since C++20) |
Deallocates storage previously allocated by a matchingoperator new oroperator new[]. These deallocation functions are called bydelete anddelete[] expressions and byplacementnew expressions to deallocate memory after destructing (or failing to construct) objects with dynamic storage duration. They may also be called using regular function call syntax.
Overloads(1-8) are implicitly declared in each translation unit even if the<new> header is not included.
Seedelete expression for the criteria of selecting overload.
Contents |
ptr | - | pointer to a memory block to deallocate or a null pointer |
sz | - | the size that was passed to the matching allocation function |
place | - | pointer used as the placement parameter in the matching placement new |
tag | - | overload disambiguation tag matching the tag used by non-throwing operator new |
al | - | alignment of the object or array element that was allocated |
args | - | arbitrary parameters matching a placement allocation function (may includestd::size_t andstd::align_val_t) |
All deallocation functions arenoexcept(true) unless specified otherwise in the declaration. | (since C++11) |
If a deallocation function terminates by throwing an exception, the behavior is undefined, even if it is declared withnoexcept(false)(since C++11).
Overloads(1-12) arereplaceable. The effects of the default versions are:
Globaloperator
snew/delete replacement:
#include <cstdio>#include <cstdlib>#include <new> // no inline, required by [replacement.functions]/3void*operator new(std::size_t sz){std::printf("1) new(size_t), size = %zu\n", sz);if(sz==0)++sz;// avoid std::malloc(0) which may return nullptr on success if(void*ptr=std::malloc(sz))return ptr; throwstd::bad_alloc{};// required by [new.delete.single]/3} // no inline, required by [replacement.functions]/3void*operator new[](std::size_t sz){std::printf("2) new[](size_t), size = %zu\n", sz);if(sz==0)++sz;// avoid std::malloc(0) which may return nullptr on success if(void*ptr=std::malloc(sz))return ptr; throwstd::bad_alloc{};// required by [new.delete.single]/3} void operator delete(void* ptr)noexcept{std::puts("3) delete(void*)");std::free(ptr);} void operator delete(void* ptr,std::size_t size)noexcept{std::printf("4) delete(void*, size_t), size = %zu\n", size);std::free(ptr);} void operator delete[](void* ptr)noexcept{std::puts("5) delete[](void* ptr)");std::free(ptr);} void operator delete[](void* ptr,std::size_t size)noexcept{std::printf("6) delete[](void*, size_t), size = %zu\n", size);std::free(ptr);} int main(){int* p1= newint; delete p1; int* p2= newint[10];// guaranteed to call the replacement in C++11 delete[] p2;}
Possible output:
// Compiled with GCC-5 in C++17 mode to obtain the following:1) op new(size_t), size = 44) op delete(void*, size_t), size = 42) op new[](size_t), size = 405) op delete[](void* ptr)
Overloads ofoperator delete andoperator delete[] with additional user-defined parameters ("placement forms",(15,16)) may be declared at global scope as usual, and are called by the matching placement forms ofnew expressions if a constructor of the object that is being allocated throws an exception.
The standard library placement forms ofoperator delete andoperator delete[](13,14) cannot be replaced and can only be customized if the placementnew expression did not use the::new syntax, by providing a class-specific placement delete(25,26) with matching signature:void T::operator delete(void*,void*) orvoid T::operator delete[](void*,void*).
Deallocation functions(17-24) may be defined as static member functions of a class. These deallocation functions, if provided, are called bydelete expressions when deleting objects(17,19,21) and arrays(18,20,22) of this class, unless the delete expression used the form::delete which bypasses class-scope lookup. The keywordstatic is optional for these function declarations: whether the keyword is used or not, the deallocation function is always a static member function.
The delete expression looks for appropriate deallocation function's name starting from the class scope (array form looks in the scope of the array element class) and proceeds to the global scope if no members are found as usual. Note, that as pername lookup rules, any deallocation functions declared in class scope hides all global deallocation functions.
If the static type of the object that is being deleted differs from its dynamic type (such as when deleting apolymorphic object through a pointer to base), and if the destructor in the static type is virtual, the single object form of delete begins lookup of the deallocation function's name starting from the point of definition of the final overrider of its virtual destructor. Regardless of which deallocation function would be executed at run time, the statically visible version ofoperator delete must be accessible in order to compile. In other cases, when deleting an array through a pointer to base, or when deleting through pointer to base with non-virtual destructor, the behavior is undefined.
If the single-argument overload(17,18) is not provided, but the size-aware overload takingstd::size_t as the second parameter(21,22) is provided, the size-aware form is called for normal deallocation, and the C++ runtime passes the size of the object to be deallocated as the second argument. If both forms are defined, the size-unaware version is called.
#include <cstddef>#include <iostream> // sized class-specific deallocation functionsstruct X{staticvoid operator delete(void* ptr,std::size_t sz){std::cout<<"custom delete for size "<< sz<<'\n';::operator delete(ptr);} staticvoid operator delete[](void* ptr,std::size_t sz){std::cout<<"custom delete for size "<< sz<<'\n';::operator delete[](ptr);}}; int main(){ X* p1= new X; delete p1; X* p2= new X[10]; delete[] p2;}
Possible output:
custom delete for size 1custom delete for size 18
Overloads ofoperator delete andoperator delete[] with additional user-defined parameters ("placement forms",(25,26)) may also be defined as class members. When the failed placementnew expression looks for the corresponding placementdelete function to call, it begins lookup at class scope before examining the global scope, and looks for the function with the signature matching the placementnew:
#include <cstddef>#include <iostream>#include <stdexcept> struct X{ X(){throwstd::runtime_error("X(): std::runtime_error");} // custom placement newstaticvoid*operator new(std::size_t sz,bool b){std::cout<<"custom placement new called, b = "<< b<<'\n';return::operator new(sz);} // custom placement deletestaticvoid operator delete(void* ptr,bool b){std::cout<<"custom placement delete called, b = "<< b<<'\n';::operator delete(ptr);}}; int main(){try{[[maybe_unused]] X* p1= new(true) X;}catch(conststd::exception& ex){std::cout<< ex.what()<<'\n';}}
Output:
custom placement new called, b = 1custom placement delete called, b = 1X(): std::runtime_error
If class-leveloperator delete is a template function, it must have the return type ofvoid, the first argumentvoid*, and it must have two or more parameters. In other words, only placement forms can be templates. A template instance is never a usual deallocation function, regardless of its signature. The specialization of the template operator delete is chosen withtemplate argument deduction.
The call to the class-specificT::operator delete on a polymorphic class is the only case where a static member function is called through dynamic dispatch.
The following functions are required to be thread-safe:
Calls to these functions that allocate or deallocate a particular unit of storage occur in a single total order, and each such deallocation callhappens-before the next allocation (if any) in this order. | (since C++11) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_sized_deallocation | 201309L | (C++14) | Sized deallocation |
__cpp_impl_destroying_delete | 201806L | (C++20) | Destroying operator delete (compiler support) |
__cpp_lib_destroying_delete | 201806L | (C++20) | Destroying operator delete (library support) |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 220 | C++98 | user-defined deallocation functions were permitted to throw | throwing from a deallocation function results in undefined behavior |
CWG 1438 | C++98 | any use of an invalid pointer value was undefined behavior | only indirection and deallocation are |
LWG 206 | C++98 | replacing(2) did not affect the default behavior of(10) | the default behavior changes accordingly |
LWG 298 | C++98 | replacing(1) did not affect the default behavior of(9) | the default behavior changes accordingly |
LWG 404 | C++98 | replacements of the replaceable deallocation functions could be declaredinline | prohibited, no diagnostic required |
LWG 2458 | C++14 | overloads taking(void*,std::size_t,const std::nothrow_t&) were specified, but could never be called | removed spurious overloads |
[static](C++23) | deallocates memory previously obtained fromoperator new (public static member function of std::generator<Ref,V,Allocator>::promise_type )[edit] |
allocation functions (function)[edit] | |
(deprecated in C++17)(removed in C++20) | frees uninitialized storage (function template)[edit] |
deallocates previously allocated memory (function)[edit] |