Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::allocator_traits<Alloc>::allocate_at_least

      From cppreference.com
      <cpp‎ |memory‎ |allocator traits
       
       
      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)
       
       
      staticconstexprstd::allocation_result<pointer, size_type>
          allocate_at_least( Alloc& a, size_type n);
      (since C++23)

      allocate_at_least callsa.allocate_at_least(n) and returns its result if the call is well-formed, otherwise, it is equivalent toreturn{a.allocate(n), n};.

      allocator_at_least tries to allocate a storage for at leastnvalue_type objects, and provides a fallback mechanism that allocates a storage for exactn objects.

      Contents

      [edit]Parameters

      a - an allocator used for allocating storage
      n - the lower bound of number of objects to allocate storage for

      [edit]Return value

      a.allocate_at_least(n) if it is well-formed.

      Otherwise,std::allocation_result<pointer, size_type>{a.allocate(n), n}.

      [edit]Exceptions

      Throws what and when the selected allocation function throws.

      [edit]Notes

      Theallocate_at_least member function ofAllocator types are mainly provided for contiguous containers, e.g.std::vector andstd::basic_string, in order to reduce reallocation by making their capacity match the actually allocated size when possible. Becauseallocate_at_least provides a fallback mechanism, it can be directly used where appropriate.

      Given an allocator objecta of typeAlloc, letresult denote the value returned fromstd::allocator_traits<Alloc>::allocate_at_least(a, n), the storage should be deallocated bya.deallocate(result.ptr, m) (typically called viastd::allocator_traits<Alloc>::deallocate(a, result.ptr, m)) in order to avoid memory leak.

      The argumentm used in deallocation must be not less thann and not greater thanresult.count, otherwise, the behavior is undefined. Note thatn is always equal toresult.count if the allocator does not provideallocate_at_least, which means thatm is required to be equal ton.

      Feature-test macroValueStdFeature
      __cpp_lib_allocate_at_least202302L(C++23)allocate_at_least etc.

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp