std::align

From cppreference.com
< cpp‎ | memory
 
 
 
Dynamic memory management
Uninitialized storage
(C++17)
(deprecated since c++17)
(deprecated since c++17)
(deprecated since c++17)
Garbage collection support
Miscellaneous
(C++11)
align
(C++11)
C Library
Low level memory management
 
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 pointer ptr to a buffer of size space, returns a pointer aligned by the specified alignment for size number of bytes and decreases space 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 returns nullptr.

The behavior is undefined if alignment is not a fundamental or extended alignment value supported by the implementation (until C++17)power of two (since C++17).

Parameters

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

Return value

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

Example

demonstrates the use of std::align to place objects of different type in memory

#include <iostream>
#include <memory>
 
template <std::size_t N>
struct MyAllocator
{
    char data[N];
    void* p;
    std::size_t sz;
    MyAllocator() : p(data), sz(N) {}
    template <typename T>
    T* aligned_alloc(std::size_t a = alignof(T))
    {
        if (std::align(a, sizeof(T), p, sz))
        {
            T* result = reinterpret_cast<T*>(p);
            p = (char*)p + sizeof(T);
            sz -= sizeof(T);
            return result;
        }
        return nullptr;
    }
};
 
int main()
{
    MyAllocator<64> a;
 
    // allocate a char
    char* p1 = a.aligned_alloc<char>();
    if (p1)
        *p1 = 'a';
    std::cout << "allocated a char at " << (void*)p1 << '\n';
 
    // allocate an int
    int* p2 = a.aligned_alloc<int>();
    if (p2)
        *p2 = 1;
    std::cout << "allocated an int at " << (void*)p2 << '\n';
 
    // allocate an int, aligned at 32-byte boundary
    int* p3 = a.aligned_alloc<int>(32);
    if (p3)
        *p3 = 2;
    std::cout << "allocated an int at " << (void*)p3 << " (32 byte alignment)\n";
}

Possible output:

allocated a char at 0x2ff21a08
allocated an int at 0x2ff21a0c
allocated an int at 0x2ff21a20 (32 byte alignment)

See also

alignof operator queries alignment requirements of a type (since C++11)
alignas specifier specifies that the storage for the variable should be aligned by specific amount (C++11)
defines the type suitable for use as uninitialized storage for types of given size
(class template)