Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::free

      From cppreference.com
      <cpp‎ |memory‎ |c
       
       
      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<cstdlib>
      void free(void* ptr);

      Deallocates the space previously allocated bystd::malloc,std::calloc,std::aligned_alloc(since C++17), orstd::realloc.

      Ifptr is a null pointer, the function does nothing.

      The behavior is undefined if the value ofptr does 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 byptr has already been deallocated, that is,std::free orstd::realloc has already been called withptr as the argument and no calls tostd::malloc,std::calloc,std::aligned_alloc(since C++17), orstd::realloc resulted in a pointer equal toptr afterwards.

      The behavior is undefined if afterstd::free returns, 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:

      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)

      Contents

      [edit]Parameters

      ptr - pointer to the memory to deallocate

      [edit]Return value

      (none)

      [edit]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 tostd::free.

      [edit]Example

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

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/c/free&oldid=179183"

      [8]ページ先頭

      ©2009-2025 Movatter.jp