Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator delete, operator delete[]

      From cppreference.com
      <cpp‎ |memory‎ |new
       
       
      Utilities library
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
       
      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.

      1-12)Replaceable deallocation functions. The standard library provides default implementations for these functions, for the effects of the default implementations, seebelow.
      1-8) Called bydelete anddelete[] expressions. Invalidates any non-nullptr.
      9-12) Called by placementnew expressions uponinitialization failure.operator delete[] invalidates any non-nullptr.
      Ifptr is not a null pointer and one of the following conditions is satisfied, the behavior is undefined:
      • Foroperator delete, the value ofptr does not represent the address of a block of memory allocated by an earlier call to (possibly replaced)operator new(std::size_t) (for overloads(1,5,9)) oroperator new(std::size_t,std::align_val_t) (for overloads(3,7,11)) which has not been invalidated by an intervening call tooperator delete.
      • Foroperator delete[], the value ofptr does not represent the address of a block of memory allocated by an earlier call to (possibly replaced)operator new[](std::size_t) (for overloads(2,6,10)) oroperator new[](std::size_t,std::align_val_t) (for overloads(4,8,12)) which has not been invalidated by an intervening call tooperator delete[].
      13,14) Called by placementnew expressions that invokednon-allocating placement allocation function when any part of the initialization in the expression terminates by throwing an exception. Performs no action.
      15-30) User-defined deallocation functions called bydelete,delete[] and placementnew expressions.
      27-30) If defined,delete expressions does not execute the destructor for*ptr before placing a call tooperator delete. Instead, direct invocation of the destructor such as byptr->~T(); becomes the responsibility of thisoperator delete.

      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

      [edit]Parameters

      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)

      [edit]Exceptions

      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).

      [edit]Global replacements

      Overloads(1-12) arereplaceable. The effects of the default versions are:

      1) Ifptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call tooperator new.
      2) Callsoperator delete(ptr) as if overload(1) can reclaim the storage allocated by the earlier call tooperator new[].
      3) Same as(1).
      4) Callsoperator delete(ptr, al) as if overload(3) can reclaim the storage allocated by the earlier call tooperator new[].
      5) Callsoperator delete(ptr).
      6) Callsoperator delete[](ptr).
      7) Callsoperator delete(ptr, al).
      8) Callsoperator delete[](ptr, al).
      9) Callsoperator delete(ptr).
      10) Callsoperator delete[](ptr).
      11) Callsoperator delete(ptr, al).
      12) Callsoperator delete[](ptr, al).

      Globaloperatorsnew/delete replacement:

      Run this code
      #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*).

      [edit]Class-specific overloads

      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.

      Run this code
      #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:

      Run this code
      #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.

      [edit]Notes

      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 macroValueStdFeature
      __cpp_sized_deallocation201309L(C++14)Sized deallocation
      __cpp_impl_destroying_delete201806L(C++20)Destroying operator delete (compiler support)
      __cpp_lib_destroying_delete201806L(C++20)Destroying operator delete (library support)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 220C++98user-defined deallocation functions were permitted to throwthrowing from a deallocation function
      results in undefined behavior
      CWG 1438C++98any use of an invalid pointer value was undefined behavioronly indirection and deallocation are
      LWG 206C++98replacing(2) did not affect the default behavior of(10)the default behavior
      changes accordingly
      LWG 298C++98replacing(1) did not affect the default behavior of(9)the default behavior
      changes accordingly
      LWG 404C++98replacements of the replaceable deallocation
      functions could be declaredinline
      prohibited, no diagnostic required
      LWG 2458C++14overloads taking(void*,std::size_t,const
      std::nothrow_t&) were specified, but could never be called
      removed spurious overloads

      [edit]See also

      [static](C++23)
      deallocates memory previously obtained fromoperator new
      (public static member function ofstd::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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/new/operator_delete&oldid=181161"

      [8]ページ先頭

      ©2009-2025 Movatter.jp