Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::allocator<T>::allocate

      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)
       
       
      (1)
      pointer allocate( size_type n,constvoid* hint=0);
      (until C++17)
      T* allocate(std::size_t n,constvoid* hint);
      (since C++17)
      (deprecated)
      (removed in C++20)
      (2)
      T* allocate(std::size_t n);
      (since C++17)
      (until C++20)
      constexpr T* allocate(std::size_t n);
      (since C++20)

      Allocatesn* sizeof(T) bytes of uninitialized storage by calling::operator new(std::size_t)or::operator new(std::size_t,std::align_val_t)(since C++17), but it is unspecified when and how this function is called. The pointerhint may be used to provide locality of reference: the allocator, if supported by the implementation, will attempt to allocate the new memory block as close as possible tohint.

      Then, this function creates an array of typeT[n] in the storage and starts its lifetime, but does not start lifetime of any of its elements.

      Use of this function is ill-formed ifT is anincomplete type.

      In order to use this function in a constant expression, the allocated storage must be deallocated within the evaluation of the same expression.

      (since C++20)

      Contents

      [edit]Parameters

      n - the number of objects to allocate storage for
      hint - pointer to a nearby memory location

      [edit]Return value

      Pointer to the first element of an array ofn objects of typeT whose elements have not been constructed yet.

      [edit]Exceptions

      Throwsstd::bad_array_new_length ifstd::numeric_limits<std::size_t>::max()/ sizeof(T)< n.

      (since C++11)

      Throwsstd::bad_alloc if allocation fails.

      [edit]Notes

      The "unspecified when and how" wording makes it possible tocombine or optimize away heap allocations made by the standard library containers, even though such optimizations are disallowed for direct calls to::operator new. For example, this is implemented by libc++ ([1] and[2]).

      After callingallocate() and before construction of elements, pointer arithmetic ofT* is well-defined within the allocated array, but the behavior is undefined if elements are accessed.

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 578C++98hint was required to be either0 or a
      pointer previously returned fromallocate()
      and not yet passed todeallocate()
      not required
      LWG 3190C++11allocate() might allocate storage of wrong sizethrowsstd::bad_array_new_length instead

      [edit]See also

      [static]
      allocates uninitialized 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/allocate&oldid=173012"

      [8]ページ先頭

      ©2009-2025 Movatter.jp