C 编程/stdalign.h
外观
< C 编程
要查询和指定对象的对其方式,请包含头文件<stdalign.h>
[1].
在<stdalign.h>
中,定义了四个宏。
- alignas
- 展开为
_Alignas
。 - alignof
- 展开为
_Alignof
。 - _alignas_is_defined
- 展开为整数常量 1。
- _alignas_is_defined
展开为整数常量 1. _alignas_is_defined
和 _alignof_is_defined
适用于与 #if
预处理指令一起使用。
alignas()
指定了声明变量时的字节对其方式 [2][3].
- 示例
#include <stdio.h> #include <stdalign.h> int main(void) { alignas(65536) int i; alignas(256) int j; int k; printf("Address of i is %p\n", &i); printf("Address of j is %p\n", &j); printf("Address of k is %p\n", &k); return 0; }
- 结果
Address of i is 0x7ffffffe0000 Address of j is 0x7ffffffeff00 Address of k is 0x7ffffffeffec
alignof()
返回指定类型在size_t
中所需的对其方式 [2][3].
- 示例
#include <stdio.h> #include <stdalign.h> int main(void) { printf("Alignment requirement for char is %zu.\n", alignof(char)); printf("Alignment requirement for int is %zu.\n", alignof(int)); printf("Alignment requirement for float is %zu.\n", alignof(float)); printf("Alignment requirement for double is %zu.\n", alignof(double)); return 0; }
- 结果
Alignment requirement for char is 1. Alignment requirement for int is 4. Alignment requirement for float is 4. Alignment requirement for double is 8.
alignas 类型说明符 (keyword_Alignas)、alignof 运算符 (keyword_Alignof)、头文件<stdalign.h>
,在 ISO/IEC 9899:2011 中添加 [1].
- ↑ a b C11: WG14/N1570 Committee Draft - April 12, 2011 ISO/IEC 9899:201x (PDF). ISO/IEC. p.268, §7.15 Alignment <stdalign.h>.
- ↑ a b jtc1/sc22/wg14/www/docs/n1570.pdf C11: WG14/N1570 Committee Draft - April 12, 2011 ISO/IEC 9899:201x (PDF). ISO/IEC. p.48, §6.2.8 Alignment of objects'.
{{cite book}}
: Check|url=
value (help) - ↑ a b 22/wg14/www/docs/n1570.pdf C11: WG14/N1570 Committee Draft - April 12, 2011 ISO/IEC 9899:201x (PDF). ISO/IEC. p.127, §6.7.5 Alignment specifier.
{{cite book}}
: Check|url=
value (help)
- ISO/IEC 9899:2018(en) 信息技术 - 编程语言 - C(2018-07-05)