Alignment support comes with the C++11 standard and it is one of the recent features of Modern C++ compilers. One part of this support is a new 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++.
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 C++ language and library with alignment-related features, which we refer to as alignment support. These alignment features include:
- An alignment specifier (
alignas
) to declarations. alignof
expression to retrieve alignment requirements of a type.- Alignment arithmetic by library support.
std::align
standard function for pointer alignment at run time.
In this post, we will explain the alignas
alignment specifier.
What is the alignof expression in modern C++?
The alignof
operator or std::alignof
keyword is an alignment support feature that comes with the C++11 standard and it is used for the alignment requirement of a type. It returns a value of type std::size_t
in bytes. In other words, the alignof keyword is required for a type_id
which is an instance of the type.
What is the syntax of the alignof in modern C++?
The alignof
keyword uses the following syntax:
1 |
alignof ( type_id ) |
The type_id
can be an array type whose element type is complete, a complete object type, or a reference type to one of these. The alignof
operator tells you the alignment of a type. The alignof keyword is one of the C++11 features added to C++Builder 2009 and greater.
The result is an integer constant of type std::size_t
. The value indicates the boundaries on which elements of that type are aligned in memory. An alignment of 2 means that the type must begin on even memory addresses, for instance. A typical value for alignof(double)
might be 8.
Applying alignof
to a reference type yields the alignment of the referenced type. If you apply alignof
to an array type, you get the alignment of the type of its element.
Is there a simple example of how to use the alignof expression in modern C++?
Here is a simple example of how you can use alignof
with a struct
type.
1 2 3 4 5 6 7 |
struct my_struct1 { char c; // 1 bytes short int i; // 2 bytes }; std::cout << alignof(my_struct1) << std::endl; |
this will output 2, that means that the type must begin on even memory addresses, for instance.
Here is another struct
example,
1 2 3 4 5 6 7 8 |
struct my_struct2 { short int i; // 2 bytes int i2; // 4 bytes long long int i3; //8 bytes }; std::cout << alignof(my_struct2) << std::endl; |
this will output 8, that means that the type must begin on 8 bytes memory addresses, for instance.
Is there a full example of how to use the alignof expression in modern C++?
Here is a full C++ example of an alignof
expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> struct my_struct1 { char c; // 1 bytes short int i; // 2 bytes }; struct my_struct2 { short int i; // 2 bytes int i2; // 4 bytes long long int i3; //8 bytes }; int main() { std::cout << alignof(my_struct1) << std::endl; std::cout << alignof(my_struct2) << 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