|
| 1 | +--- |
| 2 | +title: "Alignment" |
| 3 | +description: "Describes Microsoft Visual C type alignment" |
| 4 | +ms.date: "12/10/2020" |
| 5 | +helpviewer_keywords: ["_Alignof keyword [C]", "_Alignas keyword [C]", "memory, alignment"] |
| 6 | +--- |
| 7 | +# Alignment |
| 8 | + |
| 9 | +One of the low-level features of C is the ability to specify the precise alignment of objects in memory to take maximum advantage of the hardware architecture. |
| 10 | + |
| 11 | +CPUs read and write memory more efficiently when data is stored at an address that is a multiple of the data size. For example, a 4 byte integer is accessed more efficiently if it is stored in an address that is a multiple of 4. When data is not aligned, the CPU needs to do more work to access the data. |
| 12 | + |
| 13 | +By default, the compiler aligns data based on its size: **`char`** on 1-byte boundaries, **`short`** on 2-byte boundaries, **`int`**, **`long`**, and **`float`** on 4-byte boundaries, **`double`** on 8-byte boundaries, and so on. |
| 14 | + |
| 15 | +Additionally, by aligning frequently used data to the processor's cache line size, you improve cache performance. For example, if you define a structure whose size is less than 32 bytes, you may want 32 byte alignment to make sure that objects of that structure type are cached efficiently. |
| 16 | + |
| 17 | +Usually you don't need to be concerned with alignment because the default alignment is typically good enough. In some cases, however, you can achieve performance improvements, or memory savings, by specifying a custom alignment for your data structures. |
| 18 | + |
| 19 | +Use the C11 keywords **`_Alignof`** to get the preferred alignment of a type or variable, and **`_Alignas`** to specify custom alignment for a variable or user-defined type. |
| 20 | + |
| 21 | +The convenience macros **`alignof`** and **`alignas`**, defined in `<stdalign.h>`, map directly to **`_Alignof`** and **`_Alignas`**, respectively. |
| 22 | + |
| 23 | +## `alignas` and `_Alignas` (C11) |
| 24 | + |
| 25 | +Use **`alignas`** or **`_Alignas`** to specify custom alignment for a variable or user-defined type. It can be applied to a struct, union, enumeration, or variable. |
| 26 | + |
| 27 | +### `alignas` syntax |
| 28 | + |
| 29 | +```c |
| 30 | +alignas(type) |
| 31 | +alignas(constant-expression) |
| 32 | +_Alignas(type) |
| 33 | +_Alignas(constant-expression) |
| 34 | +``` |
| 35 | + |
| 36 | +### Remarks |
| 37 | + |
| 38 | +`_Alignas` can't be used in the declaration of a typedef, bit-field, function, function parameter, or an object declared with the `register` specifier. |
| 39 | + |
| 40 | +### `alignas` example |
| 41 | + |
| 42 | +This examples uses the convenience macro **`alignof`** because it's portable to C++. The behavior is the same if you use `_Alignof`. |
| 43 | + |
| 44 | +```c |
| 45 | +// Compile with /std:c11 |
| 46 | + |
| 47 | +#include <stdio.h> |
| 48 | +#include <stdalign.h> |
| 49 | + |
| 50 | +typedef struct |
| 51 | +{ |
| 52 | + int value; // aligns on a 4-byte boundary |
| 53 | + alignas(32) char alignedMemory[32]; // assuming a 32 byte friendly cache alignment |
| 54 | +} cacheFriendly; |
| 55 | + |
| 56 | +typedef struct |
| 57 | +{ |
| 58 | + int a; // aligns on 4-byte boundary |
| 59 | + double b; // aligns on 8-byte boundary. |
| 60 | +} test; |
| 61 | + |
| 62 | +int main() |
| 63 | +{ |
| 64 | + printf("sizeof(cacheFriendly): %d\n", sizeof(cacheFriendly)); // 4 bytes for int value + 32 bytes for alignedMemory[] + padding for alignment |
| 65 | + printf("alignof(cacheFriendly): %d\n", alignof(cacheFriendly)); // 32 due to alignedMemory[] being aligned on a 32 byte boundary |
| 66 | + |
| 67 | + /* output |
| 68 | + sizeof(cacheFriendly): 64 |
| 69 | + alignof(cacheFriendly): 32 |
| 70 | + */ |
| 71 | +``` |
| 72 | +
|
| 73 | +## `alignof` and `_Alignof` (C11) |
| 74 | +
|
| 75 | +`_Alignof` returns the alignment in bytes of the specified type. It returns a value of type `size_t`. |
| 76 | +
|
| 77 | +### `alignof` syntax |
| 78 | +
|
| 79 | +```cpp |
| 80 | +alignof(type) |
| 81 | +_Alignof(type) |
| 82 | +``` |
| 83 | + |
| 84 | +### `alignof` example |
| 85 | + |
| 86 | +This examples uses the convenience macro **`alignof`** because it's portable to C++. The behavior is the same if you use `_Alignof`. |
| 87 | + |
| 88 | +```c |
| 89 | +// Compile with /std:c11 |
| 90 | + |
| 91 | +#include <stdalign.h> |
| 92 | + |
| 93 | +int main() |
| 94 | +{ |
| 95 | + size_t alignment = alignof(short); // 2 |
| 96 | + alignment = alignof(int); // 4 |
| 97 | + alignment = alignof(long); // 4 |
| 98 | + alignment = alignof(float); // 4 |
| 99 | + alignment = alignof(double); // 8 |
| 100 | + |
| 101 | + typedef struct |
| 102 | + { |
| 103 | + int a; |
| 104 | + double b; |
| 105 | + } test; |
| 106 | + |
| 107 | + alignment = alignof(test); // returns 8 because that is the alignment requirement of the largest element in the structure |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Requirements |
| 112 | + |
| 113 | +[std:c++11](../build/reference/std-specify-language-standard-version.md) or later is required. |
| 114 | + |
| 115 | +Windows SDK version 10.0.20201.0 or later. This is currently an Insider build which you can download from [Windows Insider Preview Downloads](https://www.microsoft.com/software-download/windowsinsiderpreviewSDK). See [C11 and C17: Getting Started](https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/#c11-and-c17-getting-started) for instructions on installing and using this SDK. |
| 116 | + |
| 117 | +## See also |
| 118 | + |
| 119 | +[`/std` (Specify Language Standard Version)](../build/reference/std-specify-language-standard-version.md)\ |
| 120 | +[C++ `alignof` and `alignas`](..\cpp\alignment-cpp-declarations#alignof-and-alignas) |
| 121 | +[Compiler handling of data alignment](..\cpp\alignment-cpp-declarations#compiler-handling-of-data-alignment) |
0 commit comments