C Programming/stdalign.h
To query and specify the alignment of an object, include the header <stdalign.h>
[1].
Macro
editIn <stdalign.h>
, four macros are defined.
- alignas
- Expanded to
_Alignas
. - alignof
- Expanded to
_Alignof
. - _alignas_is_defined
- expands to integer constant 1.
- _alignas_is_defined
Expanded to integer constant 1.
_alignas_is_defined
and _alignof_is_defined
are suitable for use with #if
preprocessing directives.
alignas
editalignas()
specifies the alignment in bytes for declaring variables[2][3].
- Example
#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; }
- Result
Address of i is 0x7ffffffe0000 Address of j is 0x7ffffffeff00 Address of k is 0x7ffffffeffec
alignof
editalignof()
returns the alignment required by the specified type in size_t
[2][3].
- Example
#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; }
- Result
Alignment requirement for char is 1. Alignment requirement for int is 4. Alignment requirement for float is 4. Alignment requirement for double is 8.
History
editThe alignas type specifier (keyword_Alignas), alignof operator (keyword_Alignof), header<stdalign.h>
, added in ISO/IEC 9899:2011[1].
Footnotes
edit- ↑ 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)