Skip to content

Commit 115827f

Browse files
committed
book: add memory alignment alignof and alignas
see changkun#2
1 parent dae42fc commit 115827f

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

book/en-us/09-others.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,40 @@ Custom literals support four literals:
159159
3. String literals: A parameter table of the form `(const char *, size_t)` must be used;
160160
4. Character literals: Parameters can only be `char`, `wchar_t`, `char16_t`, `char32_t`.
161161

162+
## 9.4 Memory Alignment
163+
164+
C++ 11 introduces two new keywords, `alignof` and `alignas`, to support control of memory alignment.
165+
The `alignof` keyword can get a platform-dependent value of type `std::size_t` to query the alignment of the platform.
166+
Of course, we are sometimes not satisfied with this, and even want to customize the alignment of the structure. Similarly, C++ 11 introduces `alignas`.
167+
To reshape the alignment of a structure. Let's look at two examples:
168+
169+
```cpp
170+
#include <iostream>
171+
172+
struct Storage {
173+
char a;
174+
int b;
175+
double c;
176+
long long d;
177+
};
178+
179+
struct alignas(std::max_align_t) AlignasStorage {
180+
char a;
181+
int b;
182+
double c;
183+
long long d;
184+
};
185+
186+
int main() {
187+
std::cout << alignof(Storage) << std::endl;
188+
std::cout << alignof(AlignasStorage) << std::endl;
189+
return 0;
190+
}
191+
```
192+
193+
where `std::max_align_t` requires exactly the same alignment for each scalar type, so it has almost no difference in maximum scalars.
194+
In turn, the result on most platforms is `long double`, so the alignment requirement for `AlignasStorage` we get here is 8 or 16.
195+
162196
## Conclusion
163197
164198
Several of the features introduced in this section are those that

book/en-us/toc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
+ 9.3 Literal
9494
+ Raw String Literal
9595
+ Custom String Literal
96-
+ 9.4 Math Library
96+
+ 9.4 Memory Alignment
9797
- [**Chapter 10 Outlook: Introduction of C++20**](./10-cpp20.md)
9898
+ 10.1 Concept
9999
+ 10.2 Range

book/zh-cn/09-others.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,40 @@ int main() {
145145
3. 字符串字面量:必须使用 `(const char *, size_t)` 形式的参数表;
146146
4. 字符字面量:参数只能是 `char`, `wchar_t`, `char16_t`, `char32_t` 这几种类型。
147147

148+
## 9.4 内存对齐
149+
150+
C++ 11 引入了两个新的关键字 `alignof``alignas` 来支持对内存对齐进行控制。
151+
`alignof` 关键字能够获得一个与平台相关的 `std::size_t` 类型的值,用于查询该平台的对齐方式。
152+
当然我们有时候并不满足于此,甚至希望自定定义结构的对齐方式,同样,C++ 11 还引入了 `alignas`
153+
来重新修饰某个结构的对齐方式。我们来看两个例子:
154+
155+
```cpp
156+
#include <iostream>
157+
158+
struct Storage {
159+
char a;
160+
int b;
161+
double c;
162+
long long d;
163+
};
164+
165+
struct alignas(std::max_align_t) AlignasStorage {
166+
char a;
167+
int b;
168+
double c;
169+
long long d;
170+
};
171+
172+
int main() {
173+
std::cout << alignof(Storage) << std::endl;
174+
std::cout << alignof(AlignasStorage) << std::endl;
175+
return 0;
176+
}
177+
```
178+
179+
其中 `std::max_align_t` 要求每个标量类型的对齐方式严格一样,因此它几乎是最大标量没有差异,
180+
进而大部分平台上得到的结果为 `long double`,因此我们这里得到的 `AlignasStorage` 的对齐要求是 8 或 16。
181+
148182
## 总结
149183
150184
本节介绍的几个特性是从仍未介绍的现代 C++ 新特性里使用频次较靠前的特性了,`noexcept` 是最为重要的特性,它的一个功能在于能够阻止异常的扩散传播,有效的让编译器最大限度的优化我们的代码。

book/zh-cn/toc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
+ 9.3 字面量
9494
+ 原始字符串字面量
9595
+ 自定义字面量
96-
+ 9.4 数学库
96+
+ 9.4 内存对齐
9797
- [**第 10 章 展望: C++20 简介**](./10-cpp20.md)
9898
+ 10.1 Concept
9999
+ 10.2 Range

code/9/9.3.alignment.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
3+
struct Storage {
4+
char a;
5+
int b;
6+
double c;
7+
long long d;
8+
};
9+
10+
struct alignas(std::max_align_t) AlignasStorage {
11+
char a;
12+
int b;
13+
double c;
14+
long long d;
15+
};
16+
17+
int main() {
18+
std::cout << alignof(Storage) << std::endl;
19+
std::cout << alignof(AlignasStorage) << std::endl;
20+
return 0;
21+
}

0 commit comments

Comments
 (0)