Difference between revisions of "cpp/memory/align"
Andreas Krug (Talk | contribs) m ({{c}}, capitalized 1st letter, ., fmt) |
|||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|align}} | {{cpp/title|align}} | ||
{{cpp/memory/navbar}} | {{cpp/memory/navbar}} | ||
− | {{ddcl | since=c++11 | header=memory | | + | {{ddcl|since=c++11|header=memory| |
void* align( std::size_t alignment, | void* align( std::size_t alignment, | ||
std::size_t size, | std::size_t size, | ||
Line 8: | Line 8: | ||
}} | }} | ||
− | Given a pointer {{ | + | Given a pointer {{c|ptr}} to a buffer of size {{c|space}}, returns a pointer aligned by the specified {{c|alignment}} for {{c|size}} number of bytes and decreases {{c|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 {{c|nullptr}}. | 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 {{c|nullptr}}. | ||
− | The behavior is undefined if {{ | + | The behavior is undefined if {{c|alignment}} is not a power of two. |
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | alignment | the desired alignment }} | + | {{par|alignment|the desired alignment}} |
− | {{par | size | the size of the storage to be aligned}} | + | {{par|size|the size of the storage to be aligned}} |
− | {{par | ptr | pointer to contiguous storage of at least {{tt|space}} bytes}} | + | {{par|ptr|pointer to contiguous storage (a buffer) of at least {{tt|space}} bytes}} |
− | {{par | space | the size of the buffer in which to operate}} | + | {{par|space|the size of the buffer in which to operate}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | The adjusted value of {{ | + | The adjusted value of {{c|ptr}}, or null pointer value if the space provided is too small. |
===Example=== | ===Example=== | ||
− | {{example | | + | {{example |
− | + | |Demonstrates the use of {{tt|std::align}} to place objects of different type in memory. | |
+ | |code= | ||
#include <iostream> | #include <iostream> | ||
#include <memory> | #include <memory> | ||
− | template <std::size_t N> | + | template<std::size_t N> |
struct MyAllocator | struct MyAllocator | ||
{ | { | ||
Line 38: | Line 39: | ||
std::size_t sz; | std::size_t sz; | ||
MyAllocator() : p(data), sz(N) {} | MyAllocator() : p(data), sz(N) {} | ||
− | template <typename T> | + | template<typename T> |
T* aligned_alloc(std::size_t a = alignof(T)) | T* aligned_alloc(std::size_t a = alignof(T)) | ||
{ | { | ||
Line 55: | Line 56: | ||
{ | { | ||
MyAllocator<64> a; | MyAllocator<64> a; | ||
+ | std::cout << "allocated a.data at " << (void*)a.data | ||
+ | << " (" << sizeof a.data << " bytes)\n"; | ||
// allocate a char | // allocate a char | ||
− | char* | + | if (char* p = a.aligned_alloc<char>()) |
− | + | { | |
− | * | + | *p = 'a'; |
− | + | std::cout << "allocated a char at " << (void*)p << '\n'; | |
+ | } | ||
// allocate an int | // allocate an int | ||
− | 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 | // allocate an int, aligned at 32-byte boundary | ||
− | int* | + | if (int* p = a.aligned_alloc<int>(32)) |
− | + | { | |
− | * | + | *p = 2; |
− | + | std::cout << "allocated an int at " << (void*)p << " (32 byte alignment)\n"; | |
+ | } | ||
} | } | ||
− | + | |p = true | |
− | + | |output= | |
− | allocated a char at | + | allocated a.data at 0x7ffd0b331f80 (64 bytes) |
− | allocated an int at | + | allocated a char at 0x7ffd0b331f80 |
− | allocated an int at | + | allocated an int at 0x7ffd0b331f84 |
+ | allocated an int at 0x7ffd0b331fa0 (32 byte alignment) | ||
}} | }} | ||
=== Defect reports === | === Defect reports === | ||
{{dr list begin}} | {{dr list begin}} | ||
− | {{dr list item | wg=lwg|dr=2377|std=C++11|before={{tt|alignment}} required to be a fundamental or supported extended alignment value | after=only need to be a power of two}} | + | {{dr list item|wg=lwg|dr=2377|std=C++11|before={{tt|alignment}} required to be a fundamental or supported extended alignment value|after=only need to be a power of two}} |
{{dr list end}} | {{dr list end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/language/dsc alignof}} | + | {{dsc inc|cpp/language/dsc alignof}} |
− | {{dsc inc | cpp/language/dsc alignas}} | + | {{dsc inc|cpp/language/dsc alignas}} |
− | {{dsc inc | cpp/types/dsc aligned_storage}} | + | {{dsc inc|cpp/types/dsc aligned_storage}} |
+ | {{dsc inc|cpp/memory/dsc assume_aligned}} | ||
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 22:13, 10 June 2023
Defined in header <memory>
|
||
void* align( std::size_t alignment, std::size_t size, |
(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 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 least space bytes
|
space | - | the size of the buffer in which to operate |
[edit] Return value
The adjusted value of ptr, or null pointer value if the space provided is too small.
[edit] 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; 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)
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2377 | C++11 | alignment required to be a fundamental or supported extended alignment value
|
only need to be a power of two |
[edit] See also
alignof operator(C++11)
|
queries alignment requirements of a type |
alignas specifier(C++11)
|
specifies that the storage for the variable should be aligned by specific amount |
(C++11)(deprecated in C++23) |
defines the type suitable for use as uninitialized storage for types of given size (class template) |
(C++20) |
informs the compiler that a pointer is aligned (function template) |