std::align(3)
π NAME π
okay so this is the std::align(3) page, from the C++ Standard Libary manual (yes bestie, that spelling is straight from the header, we move π). And the name is literally std::align - std::align. That's the whole name. Iconic, concise, no notes.
β¨ Synopsis β¨
Defined in header <memory>:
void* align( std::size_t alignment,
std::size_t size, (since C++11)
void*& ptr,
std::size_t& space );
So here's the tea: you give it a pointer ptr to a buffer of size space, and it hands back a pointer aligned by the alignment you asked for, for size number of bytes β and it decreases the space argument by the number of bytes used for alignment. The first aligned address is what gets returned. Periodt. π
Real talk: 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? It does nothing and returns nullptr. No drama, just null. π
π¨ okay but listen: the behavior is undefined if alignment is not a power of two. Not "oops", not "implementation-defined", literally undefined. Do not play with that, bestie.
π Parameters π
alignmentβ the desired alignment β¨sizeβ the size of the storage to be aligned πptrβ pointer to contiguous storage (a buffer) of at leastspacebytes π«Άspaceβ the size of the buffer in which to operate πΈ
π¦ Return value π¦
The adjusted value of ptr, or a null pointer value if the space provided is too small. That's it. That's the return value. π
π Example π
This one demonstrates the use of std::align to place objects of different type in memory. It's giving tiny allocator energy, and honestly? Obsessed. π§
Run this code, bestie:
// Run this code
#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;
std::cout << "allocated a.data at " << (void*)a.data
<< " (" << sizeof a.data << " bytes)\n";
// allocate a char
if (char* p = a.aligned_alloc<char>())
{
*p = 'a';
std::cout << "allocated a char at " << (void*)p << '\n';
}
// allocate an int
if (int* p = a.aligned_alloc<int>())
{
*p = 1;
std::cout << "allocated an int at " << (void*)p << '\n';
}
// allocate an int, aligned at 32-byte boundary
if (int* p = a.aligned_alloc<int>(32))
{
*p = 2;
std::cout << "allocated an int at " << (void*)p << " (32 byte alignment)\n";
}
}
Possible output:
allocated a.data at 0x7ffd0b331f80 (64 bytes)
allocated a char at 0x7ffd0b331f80
allocated an int at 0x7ffd0b331f84
allocated an int at 0x7ffd0b331fa0 (32 byte alignment)
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. History really said "let's fix that" and we love her for it. π
LWG 2377β applied to C++11. Behavior as published: alignment required to be a fundamental or supported extended alignment value. Correct behavior: only need to be a power of two. Slay for clearing that up. β¨
π₯ See also π₯
alignofoperator(C++11) β queries alignment requirements of a typealignasspecifier(C++11) β specifies that the storage for the variable should be aligned by specific amountaligned_storage(C++11)(deprecated in C++23) β defines the type suitable for use as uninitialized storage for types of given size (class template)assume_aligned(C++20) β informs the compiler that a pointer is aligned (function template)
catch you later, bestie π«Ά this little page came from http://cppreference.com, dated 2024.06.10, and it's the std::align(3) page. bye π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms