Skip to content

Commit 19bf44d

Browse files
TylerMSFTTylerMSFT
authored andcommitted
1st draft
1 parent 97d5590 commit 19bf44d

5 files changed

Lines changed: 126 additions & 99 deletions

File tree

docs/c-language/alignment-c.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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)

docs/c-language/alignment.md

Lines changed: 0 additions & 94 deletions
This file was deleted.

docs/c-language/c-keywords.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ The C language uses the following keywords:
5454
:::column-end:::
5555
:::column:::
5656
**`while`**\
57-
**`_Alignas`** <sup>2, a</sup>\
58-
**`_Alignof`** <sup>2, a</sup>\
57+
**[`_Alignas`](alignment-c.md)** <sup>2, a</sup>\
58+
**[`_Alignof`](alignment-c.md)** <sup>2, a</sup>\
5959
**`_Atomic`** <sup>2, b</sup>\
6060
**`_Bool`** <sup>1, a</sup>\
6161
**`_Complex`** <sup>1, b</sup>\

docs/c-language/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
- name: Declarations and types
140140
items:
141141
- name: Alignment
142-
href: ../c-language/alignment.md
142+
href: ../c-language/alignment-c.md
143143
- name: Declarations and types
144144
href: ../c-language/declarations-and-types.md
145145
- name: Overview of declarations

docs/c-runtime-library/tgmath.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Type-generic math"
33
description: "Describes macros in <tgmath.h> that make it easier to write C code that calls the right math function, based on argument type."
44
ms.topic: "conceptual"
5-
ms.date: "9/3/2020"
5+
ms.date: "12/10/2020"
66
helpviewer_keywords: ["CRT tgmath.h"]
77
---
88

@@ -87,7 +87,7 @@ The following table lists the macros that are available in \<tgmath.h> and what
8787

8888
[std:c++11](../build/reference/std-specify-language-standard-version.md) or later is required.
8989

90-
Windows SDK version 10.0.20201.0 or later.
90+
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.
9191

9292
## See also
9393

0 commit comments

Comments
 (0)