diff --git a/book/en-us/09-others.md b/book/en-us/09-others.md index cae1dba..63f2da0 100644 --- a/book/en-us/09-others.md +++ b/book/en-us/09-others.md @@ -159,6 +159,40 @@ Custom literals support four literals: 3. String literals: A parameter table of the form `(const char *, size_t)` must be used; 4. Character literals: Parameters can only be `char`, `wchar_t`, `char16_t`, `char32_t`. +## 9.4 Memory Alignment + +C++ 11 introduces two new keywords, `alignof` and `alignas`, to support control of memory alignment. +The `alignof` keyword can get a platform-dependent value of type `std::size_t` to query the alignment of the platform. +Of course, we are sometimes not satisfied with this, and even want to customize the alignment of the structure. Similarly, C++ 11 introduces `alignas`. +To reshape the alignment of a structure. Let's look at two examples: + +```cpp +#include + +struct Storage { + char a; + int b; + double c; + long long d; +}; + +struct alignas(std::max_align_t) AlignasStorage { + char a; + int b; + double c; + long long d; +}; + +int main() { + std::cout << alignof(Storage) << std::endl; + std::cout << alignof(AlignasStorage) << std::endl; + return 0; +} +``` + +where `std::max_align_t` requires exactly the same alignment for each scalar type, so it has almost no difference in maximum scalars. +In turn, the result on most platforms is `long double`, so the alignment requirement for `AlignasStorage` we get here is 8 or 16. + ## Conclusion Several of the features introduced in this section are those that diff --git a/book/en-us/toc.md b/book/en-us/toc.md index 0cfb305..c1daacd 100644 --- a/book/en-us/toc.md +++ b/book/en-us/toc.md @@ -93,7 +93,7 @@ + 9.3 Literal + Raw String Literal + Custom String Literal - + 9.4 Math Library + + 9.4 Memory Alignment - [**Chapter 10 Outlook: Introduction of C++20**](./10-cpp20.md) + 10.1 Concept + 10.2 Range diff --git a/book/zh-cn/09-others.md b/book/zh-cn/09-others.md index 94e6d97..0e33789 100644 --- a/book/zh-cn/09-others.md +++ b/book/zh-cn/09-others.md @@ -145,6 +145,40 @@ int main() { 3. 字符串字面量:必须使用 `(const char *, size_t)` 形式的参数表; 4. 字符字面量:参数只能是 `char`, `wchar_t`, `char16_t`, `char32_t` 这几种类型。 +## 9.4 内存对齐 + +C++ 11 引入了两个新的关键字 `alignof` 和 `alignas` 来支持对内存对齐进行控制。 +`alignof` 关键字能够获得一个与平台相关的 `std::size_t` 类型的值,用于查询该平台的对齐方式。 +当然我们有时候并不满足于此,甚至希望自定定义结构的对齐方式,同样,C++ 11 还引入了 `alignas` +来重新修饰某个结构的对齐方式。我们来看两个例子: + +```cpp +#include + +struct Storage { + char a; + int b; + double c; + long long d; +}; + +struct alignas(std::max_align_t) AlignasStorage { + char a; + int b; + double c; + long long d; +}; + +int main() { + std::cout << alignof(Storage) << std::endl; + std::cout << alignof(AlignasStorage) << std::endl; + return 0; +} +``` + +其中 `std::max_align_t` 要求每个标量类型的对齐方式严格一样,因此它几乎是最大标量没有差异, +进而大部分平台上得到的结果为 `long double`,因此我们这里得到的 `AlignasStorage` 的对齐要求是 8 或 16。 + ## 总结 本节介绍的几个特性是从仍未介绍的现代 C++ 新特性里使用频次较靠前的特性了,`noexcept` 是最为重要的特性,它的一个功能在于能够阻止异常的扩散传播,有效的让编译器最大限度的优化我们的代码。 diff --git a/book/zh-cn/toc.md b/book/zh-cn/toc.md index 9da4274..12f93af 100644 --- a/book/zh-cn/toc.md +++ b/book/zh-cn/toc.md @@ -93,7 +93,7 @@ + 9.3 字面量 + 原始字符串字面量 + 自定义字面量 - + 9.4 数学库 + + 9.4 内存对齐 - [**第 10 章 展望: C++20 简介**](./10-cpp20.md) + 10.1 Concept + 10.2 Range diff --git a/code/9/9.3.alignment.cpp b/code/9/9.3.alignment.cpp new file mode 100644 index 0000000..0cd57eb --- /dev/null +++ b/code/9/9.3.alignment.cpp @@ -0,0 +1,21 @@ +#include + +struct Storage { + char a; + int b; + double c; + long long d; +}; + +struct alignas(std::max_align_t) AlignasStorage { + char a; + int b; + double c; + long long d; +}; + +int main() { + std::cout << alignof(Storage) << std::endl; + std::cout << alignof(AlignasStorage) << std::endl; + return 0; +} \ No newline at end of file