Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::allocator<T>::deallocate

      From cppreference.com
      <cpp‎ |memory‎ |allocator
       
       
      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)
       
       
      void deallocate( T* p,std::size_t n);
      (constexpr since C++20)

      Deallocates the storage referenced by the pointerp, which must be a pointer obtained by an earlier call toallocate() orallocate_at_least()(since C++23).

      The argumentn must be equal to the first argument of the call toallocate() that originally producedp, or in the range[mcount] ifp is obtained from a call toallocate_at_least(m) which returned{p, count}(since C++23); otherwise, the behavior is undefined.

      Calls::operator delete(void*)or::operator delete(void*,std::align_val_t)(since C++17), but it is unspecified when and how it is called.

      In evaluation of a constant expression, this function must deallocate storage allocated within the evaluation of the same expression.

      (since C++20)

      Contents

      [edit]Parameters

      p - pointer obtained fromallocate() orallocate_at_least()(since C++23)
      n - number of objects earlier passed toallocate(), or a number between requested and actually allocated number of objects viaallocate_at_least() (may be equal to either bound)(since C++23)

      [edit]Return value

      (none)

      [edit]Example

      Run this code
      #include <algorithm>#include <cstddef>#include <iostream>#include <memory>#include <string> class S{inlinestaticint n{1};int m{};void pre()const{std::cout<<"#"<< m<<std::string(m,' ');}public:    S(int x): m{n++}{ pre();std::cout<<"S::S("<< x<<");\n";}    ~S(){ pre();std::cout<<"S::~S();\n";}void id()const{ pre();std::cout<<"S::id();\n";}}; int main(){constexprstd::size_t n{4};std::allocator<S> allocator;try{        S* s= allocator.allocate(n);// may throwfor(std::size_t i{}; i!= n;++i){//  allocator.construct(&s[i], i + 42); // removed in C++20std::construct_at(&s[i], i+42);// since C++20}std::for_each_n(s, n,[](constauto& e){ e.id();});std::destroy_n(s, n);        allocator.deallocate(s, n);}catch(std::bad_array_new_lengthconst& ex){std::cout<< ex.what()<<'\n';}catch(std::bad_allocconst& ex){std::cout<< ex.what()<<'\n';}}

      Output:

      #1 S::S(42);#2  S::S(43);#3   S::S(44);#4    S::S(45);#1 S::id();#2  S::id();#3   S::id();#4    S::id();#1 S::~S();#2  S::~S();#3   S::~S();#4    S::~S();

      [edit]See also

      allocates uninitialized storage
      (public member function)[edit]
      allocates uninitialized storage at least as large as requested size
      (public member function)[edit]
      [static]
      deallocates storage using the allocator
      (public static member function ofstd::allocator_traits<Alloc>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/allocator/deallocate&oldid=163383"

      [8]ページ先頭

      ©2009-2025 Movatter.jp