Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Learn About The Powerful Alignment Support Functions Of Modern C++

Learn About The Powerful Alignment Support Functions Of Modern C++

One of the features of Modern C++ is alignment support which was introduced with the C++11 standard. Most of the alignment support features can be used with modern C++ compilers that support one of the ISO Standards of C++11, C++14, C++17, and C++20. In this post, we explain what is alignment support and how we can use alignas, alignof, std::align, aligned_union, aligned_storage, and aligned_alloc in Modern C++.

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. This is not an aesthetic behavior like aligning the edges of a stack of playing cards but has some specific practical advantages the contribute to your program’s efficiency.

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 C++ language and library with alignment-related features, known as alignment support. These alignment features include:

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/.

What are the alignment support features in modern C++?

One of the features of this alignment support was the introduction of a new keyword alignas which is the alignment specifier. For example, we can apply the alignas alignment specifier to define the minimum block size of data types such as the size of structs. In the following post, we explain how we can use alignas in Modern C++ .

Another feature of this alignment support is the keyword alignof which is used for the alignment requirement of a type. In this post, we explain how we can use alignof in Modern C++

C++ alignment support also has a keyword align std::align which is used to get a pointer aligned by the specified alignment for a size in bytes of the buffer. In the next post, we explain how we can use align in Modern C++.

The keyword align std::aligned_union provides a nested type which can be used as uninitialized storage for any object whose size is at most the object size stated by the alignment size requirement. In this post below, we explain how we can use align_union in Modern C++.

When it comes to nested types the align std::aligned_storage keywords are used to provide a nested type which can be used as an uninitialized storage for any object whose size is at most given object size by the alignment size requirement. We explain how we can use aligned_storage in Modern C++ in the following post.

Finally, to round off our collection of alignment support articles the keyword align std::aligned_alloc is described in the following post.

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.

Exit mobile version