Controlling and querying object alignment
C++11 provides standardized methods for specifying and querying the alignment requirements of a type (something that was previously possible only through compiler-specific methods). Controlling the alignment is important in order to boost performance on different processors and enable the use of some instructions that only work with data on particular alignments. For example, Intel SSE and Intel SSE2 require 16 bytes alignment of data, whereas for Intel Advanced Vector Extensions (or Intel AVX), it is highly recommended to use 32 bytes alignment. This recipe explores the alignas
specifier for controlling the alignment requirements and the alignof
operator that retrieves the alignment requirements of a type.
Getting ready
You should be familiar with what data alignment is and the way the compiler performs default data alignment. However, basic information about the latter is provided in the How it works... section.
How to do it...
- To control the alignment of a type (both at the class level or data member level) or an object, use the
alignas
specifier:
struct alignas(4) foo { char a; char b; }; struct bar { alignas(2) char a; alignas(8) int b; }; alignas(8) int a; alignas(256) long b[4];
- To query the alignment of a type, use the
alignof
operator:
auto align = alignof(foo);
How it works...
Processors do not access memory one byte at a time, but in larger chunks of powers of twos (2, 4, 8, 16, 32, and so on). Owing to this, it is important that compilers align data in memory so that it can be easily accessed by the processor. Should this data be misaligned, the compiler has to do extra work for accessing data; it has to read multiple chunks of data, shift, and discard unnecessary bytes and combine the rest together.
C++ compilers align variables based on the size of their data type: 1 byte for bool
and char
, 2 bytes for short
, 4 bytes for int
, long
and float
, 8 bytes for double
and long long
, and so on. When it comes to structures or unions, the alignment must match the size of the largest member in order to avoid performance issues. To exemplify, let's consider the following data structures:
struct foo1 // size = 1, alignment = 1 { char a; }; struct foo2 // size = 2, alignment = 1 { char a; char b; }; struct foo3 // size = 8, alignment = 4 { char a; int b; };
foo1
and foo2
have different sizes, but the alignment is the same--that is, 1--because all data members are of type char
, which has a size of 1. In structure foo3
, the second member is an integer, whose size is 4. As a result, the alignment of members of this structure is done at addresses that are multiples of 4. To achieve that, the compiler introduces padding bytes. The structure foo3
is actually transformed into the following:
struct foo3_ { char a; // 1 byte char _pad0[3]; // 3 bytes padding to put b on a 4-byte boundary int b; // 4 bytes };
Similarly, the following structure has a size of 32 bytes and an alignment of 8; that is because the largest member is a double
whose size is 8. This structure, however, requires padding in several places to make sure that all members can be accessed at addresses that are multiples of 8:
struct foo4 { int a; char b; float c; double d; bool e; };
The equivalent structure created by the compiler is as follows:
struct foo4_ { int a; // 4 bytes char b; // 1 byte char _pad0[3]; // 3 bytes padding to put c on a 8-byte boundary float c; // 4 bytes char _pad1[4]; // 4 bytes padding to put d on a 8-byte boundary double d; // 8 bytes bool e; // 1 byte char _pad2[7]; // 7 bytes padding to make sizeof struct multiple of 8 };
In C++11, specifying the alignment of an object or type is done using the alignas
specifier. This can take either an expression (an integral constant expression that evaluates to 0 or a valid value for an alignment), a type-id, or a parameter pack. The alignas
specifier can be applied to the declaration of a variable or a class data member that does not represent a bit field, or to the declaration of a class, union, or enumeration. The type or object on which an alignas
specification is applied will have the alignment requirement equal to the largest, greater than zero, expression of all alignas
specifications used in the declaration.
There are several restrictions when using the alignas
specifier:
- The only valid alignments are the powers of two ( 1, 2, 4, 8, 16, 32, and so on). Any other values are illegal, and the program is considered ill-formed; that doesn't necessarily have to produce an error, as the compiler may choose to ignore the specification.
- An alignment of 0 is always ignored.
- If the largest
alignas
on a declaration is smaller than the natural alignment without anyalignas
specifier, then the program is also considered ill-formed.
In the following example, the alignas
specifier is applied on a class declaration. The natural alignment without the alignas
specifier would have been 1, but with alignas(4)
it becomes 4:
struct alignas(4) foo { char a; char b; };
In other words, the compiler transforms the preceding class into the following:
struct foo { char a; char b; char _pad0[2]; };
The alignas
specifier can be applied both on the class declaration and the member data declarations. In this case, the strictest (that is, largest) value wins. In the following example, member a
has a natural size of 1 and requires an alignment of 2; member b
has a natural size of 4 and requires an alignment of 8, therefore, the strictest alignment would be 8. The alignment requirement of the entire class is 4, which is weaker (that is, smaller) than the strictest required alignment and therefore it will be ignored, though the compiler will produce a warning:
struct alignas(4) foo { alignas(2) char a; alignas(8) int b; };
The result is a structure that looks like this:
struct foo { char a; char _pad0[7]; int b; char _pad1[4]; };
The alignas
specifier can also be applied on variables. In the next example, variable a
, that is an integer, is required to be placed in memory at a multiple of 8. The next variable, the array of 4 a
, that is an integer, is required to be placed in memory at a multiple of 8. The next variable, the array of 4 long
s, is required to be placed in memory at a multiple of 256. As a result, the compiler will introduce up to 244 bytes of padding between the two variables (depending on where in memory, at an address multiple of 8, the variable a
is located):
alignas(8) int a; alignas(256) long b[4]; printf("%pn", &a); // eg. 0000006C0D9EF908 printf("%pn", &b); // eg. 0000006C0D9EFA00
Looking at the addresses, we can see that the address of a
is indeed a multiple of 8, and the address of b
is a multiple of 256 (hexadecimal 100).
To query the alignment of a type, we use the alignof
operator. Unlike sizeof
, this operator can only be applied to type-ids, and not on variables or class data members. The types on which it can be applied can be complete types, an array type, or a reference type. For arrays, the value returned is the alignment of the element type; for references, the value returned is the alignment of the referenced type. Here are several examples:
Expression | Evaluation |
| 1, because the natural alignment of |
| 4, because the natural alignment of |
| 4 on 32-bit, 8 on 64-bit, the alignment for pointers |
| 4, because the natural alignment of the element type is 4 |
| 8, because the specified alignment for class |