std::alloca...ts::allocate(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::alloca...ts::allocate(3) πŸ’…

okay bestie, welcome to the C++ Standard Libary manual page for std::allocator_traits::allocate β€” yes, the manual spells it β€œLibary,” and we are not fixing it, periodt πŸ’–


🌸 NAME 🌸

std::allocator_traits::allocate β€” std::allocator_traits::allocate

It literally says the same thing twice. Iconic recursive main character energy, no cap 🫢


✨ Synopsis ✨

πŸ¦‹ Defined in header <memory> πŸ¦‹

(1) (since C++11) (until C++20)

static pointer allocate( Alloc& a, size_type n );

(1) (since C++20)

[[nodiscard]] static constexpr pointer allocate( Alloc& a, size_type n );

(2) (since C++11) (until C++20)

static pointer allocate( Alloc& a, size_type n,
                         const_void_pointer hint );

(2) (since C++20)

[[nodiscard]] static constexpr pointer allocate(
    Alloc& a, size_type n,
    const_void_pointer hint );

Uses the allocator a to allocate n * sizeof(Alloc::value_type) bytes of uninitialized storage. An array of type Alloc::value_type[n] is created in the storage, but none of its elements are constructed. 🧚✨

  1. Calls a.allocate(n).
  2. Additionally passes memory locality hint hint. Calls a.allocate(n, hint) if possible. If not possible (e.g. a has no two-argument member function allocate), calls a.allocate(n).

πŸŽ€ Parameters πŸŽ€

  • a β€” allocator to use 🫢
  • n β€” the number of objects to allocate storage for πŸ’«
  • hint β€” pointer to a nearby memory location πŸ“

πŸ’– Return value πŸ’–

The pointer returned by the call to a.allocate(n). That's the whole vibe check, bestie β€” pointer goes out, pointer comes back πŸ”₯


πŸ¦‹ Notes πŸ¦‹

Alloc::allocate was not required to create array object until P0593R6, which made using non-default allocator for std::vector and some other containers not well-defined according to a strict reading of the core language specification.

After calling allocate and before construction of elements, pointer arithmetic of Alloc::value_type* is well-defined within the allocated array, but the behavior is undefined if elements are accessed. 😭

bestie, that last bit is a real warning: pointer arithmetic inside the allocated array is fine before elements are constructed, but accessing elements is undefined behavior. no cap, do not touchy. πŸ’€

🌈 Example 🌈

This section is incomplete

Reason: no example

literally β€œno example” 😭 it's giving unfinished homework energy, but that's what the page says, so we keep it. πŸ“


πŸ‘‘ See also πŸ‘‘

  • allocate allocates uninitialized storage (public member function of std::allocator<T>) ✨

πŸ—‚οΈ Category πŸ—‚οΈ

  • Todo no example

kisses, bestie πŸ’‹ this page is from the C++ Standard Libary manual; source: http://cppreference.com, version/date: 2024.06.10. slay and stay safe out there with your uninitialized storage πŸŽ€πŸ¦‹πŸ’œ

πŸ“¦ where this page comes from