The C++11 standard introduced alignment support as one of the many features of a Modern compiler for the C++ programming language. One of the new features of this support is a new keyword align
std::align
which is used to get a pointer aligned by the specified alignment for a size in bytes in consider with size of the buffer. In this post, we explain how we can use align
in Modern C++.
Table of Contents
What is alignment support in modern C++?
When we talk about ‘alignment’ in C++ it means a set of hints or instructions that tell the compiler to place the physical representation of data structures and variables in memory so that they line up at specific intervals of bytes – the underlaying digital representation of all data items. Alignment tells the compiler and linker to add additional ‘space’ to a value in memory so that the next object begins ‘nicely’ on a particular memory boundary.
Due to the way the CPU and other logic chips in a computer work, alignment can help create more computationally efficient programs. Think of it in the way you cut a cake – if you use an even number of slices for your cake it’s much easier and quicker to divide it up because you can cut the cake into nice even chunks of two. If you get an odd number of cake slices, it’s a lot harder to work out how big those slices need to be to make sure you don’t have a huge slice of cake left over – or someone gets a tiny slice (or none) and stays hungry! By cutting the cake in half each time you are ‘aligning’ your cake slices evenly and optimally so all cake is efficiently used with no wasted cake.
The C++11 standard intends to extend the standard language and library with alignment-related features, known as alignment support. These alignment features include:
- The alignment specifier
alignas
for declarations. - The
alignof
expression to retrieve alignment requirements of a type. - Alignment arithmetic by library support (
aligned_storage
,aligned_union
). - std::align standard function for pointer alignment at run time.
Alignment support in C++ can be found in more detail here in the C++ standards [Note: PDF link]. We also discuss the alignof
keyword in this blog post: https://learncplusplus.org/what-is-the-alignof-expression-in-modern-c/.
In this post, we will explain the align
alignment specifier.
What is the std::align in modern C++?
std::align
is an alignment support feature that comes with C++11. It indicates the desired alignment, the size of the storage to be aligned, and a pointer ptr
to a buffer size. This method returns a pointer aligned by the specified alignment
for size
in bytes and decreases space
argument by the number of bytes used for alignment.
1 2 3 4 |
void* align( std::size_t alignment, std::size_t element_size, void*& ptr, std::size_t& space_size ); |
here:
- alignment is the requested alignment in bytes
- element_size is the size of the element to be aligned in bytes
- ptr is the pointer of contiguous storage of at least space_size bytes
- space_size is the size of the buffer to consider for alignment in bytes
Now, let see this in a simple example of how to use std::align
in C++.
Is there a simple example to std::align in modern C++?
Here is a simple example of how to use std::align
in C++
1 2 3 4 5 6 7 8 9 |
char buf[] = "abcdefghij"; void *p = buf; // pointer std::size_t space_size = sizeof(buf)-1; //maximum size for alignment if ( std::align( 7, 1, p, space_size ) ) { std::cout << static_cast<char*>(p)[0]; } |
Here is another example of using std::align in C++.
1 2 3 4 5 6 7 8 9 10 11 12 |
char buf[] = "abcdefghij"; int a = 7; // alignment int s = 1; // size of element void *p = buf; // pointer std::size_t space_size = sizeof(buf)-1; //maximum size for alignment if ( std::align( a, s, p, space_size ) ) { // char at the adress of pointer std::cout << static_cast<char*>(p)[0]; } |
As above this example will print out ‘d
‘, because ‘d’ is the pointer of contiguous storage of at least 10 bytes when the alignment is 7 and the element size is 1.
Is there a full example of how to use std::align in modern C++?
Here is a full example of how to use std::align
in modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> #include <memory> char buf[] = "abcdefghijklmnopqrstuwxyz"; int a = 7; // alignment int s = 1; // size of element void *p = buf; // pointer std::size_t space_size = sizeof(buf)-1; //maximum size for alignment int main() { std::cout << buf << std::endl; std::cout << "size of buffer:" << sizeof(buf)-1 << std::endl; while ( std::align( a, s, p, space ) ) { char* c = static_cast<char*>(p); std::cout << ":" << *c << std::endl; *c = '.'; // set char value at the pointer adress space-= s; // reduce used size p = c+1; // continue from the next char } std::cout << buf << std::endl; system("pause"); return 0; } |
If you need more in-depth details about this feature, see the original Alignment Support Proposal document.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition