Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      delete expression

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      delete expression
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Expressions
      General
      Literals
      Operators
      Conversions
       

      Destroys object(s) previously allocated by thenew-expression and releases obtained memory area.

      Contents

      [edit]Syntax

      ::(optional)delete  expression (1)
      ::(optional)delete[]expression (2)
      expression - one of the following:
      1) Destroys one non-array object created by anew-expression.
      2) Destroys an array created by anew[]-expression.

      [edit]Explanation

      Given the pointer evaluated fromexpression (after possible conversions) asptr.

      1)ptr must be one of
      • a null pointer,
      • a pointer to a non-array object created by anew-expression, or
      • a pointer to a base subobject of a non-array object created by anew-expression.
      The pointed-to type ofptr must besimilar to the type of the object (or of a base subobject). Ifptr is anything else, including if it is a pointer obtained by the array form ofnew-expression, the behavior isundefined.
      2)ptr must be a null pointer or a pointer whose value is previously obtained by an array form ofnew-expression whoseallocation function was not a non-allocating form (i.e. overload(10)).
      The pointed-to type ofptr must besimilar to the element type of the array object. Ifptr is anything else, including if it is a pointer obtained by the non-array form ofnew-expression, the behavior isundefined.

      The result of the delete-expression always has typevoid.

      If the object being deleted has incomplete class type at the point of deletion,and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined(until C++26)the program is ill-formed(since C++26).

      Ifptr is not a null pointer and thedeallocation function is not a destroying delete(since C++20), the delete-expression invokes thedestructor (if any) for the object that is being destroyed, or for every element of the array being destroyed (proceeding from the last element to the first element of the array). The destructor must beaccessible from the point where the delete-expression appears.

      After that, whether or not an exception was thrown by any destructor, the delete-expression invokes thedeallocation function: eitheroperator delete (first version) oroperator delete[] (second version), unless the matching new-expression was combined with another new-expression(since C++14).

      The deallocation function's name islooked up in the scope of the dynamic type of the object pointed to byptr, which means class-specific deallocation functions, if present, are found before the global ones. If:: is present in the delete-expression, only the global namespace is examined by this lookup. In any case, any declarations other than of usual deallocation functions are discarded.

      If any deallocation function is found, the function to be called is selected as follows (seedeallocation function for a more detailed description of these functions and their effects):

      • If at least one of the deallocation functions is a destroying delete, all non-destroying deletes are ignored.
      (since C++20)
      • If the type's alignment requirement exceeds__STDCPP_DEFAULT_NEW_ALIGNMENT__, alignment-aware deallocation functions (with a parameter of typestd::align_val_t) are preferred. For other types, the alignment-unaware deallocation functions (without a parameter of typestd::align_val_t) are preferred.
      • If more than one preferred functions are found, only preferred functions are considered in the next step.
      • If no preferred functions are found, the non-preferred ones are considered in the next step.
      • If only one function is left, that function is selected.
      (since C++17)
      • If the deallocation functions that were found are class-specific, size-unaware class-specific deallocation function (without a parameter of typestd::size_t) is preferred over size-aware class-specific deallocation function (with a parameter of typestd::size_t).
      • Otherwise, lookup reached global scope, and:
      • If the type is complete and if, for the array form only, the operand is a pointer to a class type with a non-trivial destructor or a (possibly multi-dimensional) array thereof, the global size-aware global function (with a parameter of typestd::size_t) is selected.
      • Otherwise, it is unspecified whether the global size-aware deallocation function (with a parameter of typestd::size_t) or the global size-unaware deallocation function (without a parameter of typestd::size_t) is selected.
      (since C++14)

      The selected deallocation function must beaccessible from the point where the delete-expression appears, unless the deallocation function is selected at the point of definition of thedynamic type’svirtual destructor.

      The pointer to the block of storage to be reclaimed is passed to thedeallocation function that was selected by the process above as the first argument. The size of the block is passed as the optionalstd::size_t argument.The alignment requirement is passed as the optionalstd::align_val_t argument.(since C++17)

      Ifptr is a null pointer value, no destructors are called, andthe deallocation function may or may not be called (it's unspecified), but the default deallocation functions are guaranteed to do nothing when passed a null pointer.

      Ifptr is a pointer to a base class subobject of the object that was allocated withnew, the destructor of the base class must bevirtual, otherwise the behavior is undefined.

      [edit]Notes

      A pointer tovoid cannot be deleted because it is not a pointer to an object type.

      Because a pair of brackets following the keyworddelete is always interpreted as the array form of a delete-expression, alambda-expression with an empty capture list immediately afterdelete must be enclosed in parentheses.

      // delete []{ return new int; }(); // parse errordelete([]{return newint;})();// OK
      (since C++11)

      [edit]Keywords

      delete

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 288C++98for the first form, the static type of the
      operand was compared with its dynamic type
      compare the static type of the object
      to be deleted with its dynamic type
      CWG 353C++98whether the deallocation function will be invoked if
      the destructor throws an exception was unspecified
      always invoked
      CWG 599C++98the first form could take a null pointer of
      any type, including function pointers
      except pointers to object types,
      all other pointer types are rejected
      CWG 1642C++98expression could be a pointer lvaluenot allowed
      CWG 2474C++98deleting a pointer to an object of a similar but
      different type resulted in undefined behavior
      made well-defined
      CWG 2624C++98pointers obtained from non-allocating
      operator new[] could be passed todelete[]
      prohibited
      CWG 2758C++98it was unclear how access control was done for
      the deallocation function and the destructor
      made clear

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/delete&oldid=173711"

      [8]ページ先頭

      ©2009-2025 Movatter.jp