Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::align

      From cppreference.com
      <cpp‎ |memory
       
       
      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<memory>
      void* align(std::size_t alignment,

                   std::size_t size,
                   void*& ptr,

                   std::size_t& space);
      (since C++11)

      Given a pointerptr to a buffer of sizespace, returns a pointer aligned by the specifiedalignment forsize number of bytes and decreasesspace argument by the number of bytes used for alignment. The first aligned address is returned.

      The function modifies the pointer only if it would be possible to fit the wanted number of bytes aligned by the given alignment into the buffer. If the buffer is too small, the function does nothing and returnsnullptr.

      The behavior is undefined ifalignment is not a power of two.

      Contents

      [edit]Parameters

      alignment - the desired alignment
      size - the size of the storage to be aligned
      ptr - pointer to contiguous storage (a buffer) of at leastspace bytes
      space - the size of the buffer in which to operate

      [edit]Return value

      The adjusted value ofptr, or null pointer value if the space provided is too small.

      [edit]Example

      Demonstrates the use ofstd::align to place objects of different type in memory.

      Run this code
      #include <iostream>#include <memory>#include <new> template<std::size_t N>struct MyAllocator{std::byte data[N];std::size_t sz{N};void* p{data};     MyAllocator()=default; // Note: only well-defined for implicit-lifetime typestemplate<typename T>    T* implicit_aligned_alloc(std::size_t a= alignof(T)){if(std::align(a, sizeof(T), p, sz)){            T* result=std::launder(reinterpret_cast<T*>(p));            p=static_cast<std::byte*>(p)+ sizeof(T);            sz-= sizeof(T);return result;}return nullptr;}}; int main(){    MyAllocator<64> a;std::cout<<"allocated a.data at "<<(void*)a.data<<" ("<< sizeof a.data<<" bytes)\n"; // Allocate a charif(char* p= a.implicit_aligned_alloc<char>()){*p='a';std::cout<<"allocated a char at "<<(void*)p<<'\n';} // Allocate an intif(int* p= a.implicit_aligned_alloc<int>()){*p=1;std::cout<<"allocated an int at "<<(void*)p<<'\n';} // Allocate an int, aligned at a 32-byte boundaryif(int* p= a.implicit_aligned_alloc<int>(32)){*p=2;std::cout<<"allocated an int at "<<(void*)p<<" (32-byte alignment)\n";}}

      Possible output:

      allocated a.data at 0x7ffc654e8530 (64 bytes)allocated a char at 0x7ffc654e8530allocated an int at 0x7ffc654e8534allocated an int at 0x7ffc654e8540 (32-byte alignment)

      [edit] Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2377C++11alignment required to be a fundamental or supported extended alignment valueonly need to be a power of two

      [edit]See also

      alignof(C++11) queries alignment requirements of a type
      (operator)[edit]
      alignas(C++11) specifies that the storage for the variable should be aligned by specific amount
      (specifier)[edit]
      (since C++11)(deprecated in C++23)
      defines the type suitable for use as uninitialized storage for types of given size
      (class template)[edit]
      informs the compiler that a pointer is aligned
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/align&oldid=181870"

      [8]ページ先頭

      ©2009-2025 Movatter.jp