]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gcc/extensions-to-the-c-language-family/determining-the-alignment-of-functions-types-or-variables.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gcc / extensions-to-the-c-language-family / determining-the-alignment-of-functions-types-or-variables.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. index:: alignment, type alignment, variable alignment
7
8 .. _alignment:
9
10 Determining the Alignment of Functions, Types or Variables
11 **********************************************************
12
13 The keyword ``__alignof__`` determines the alignment requirement of
14 a function, object, or a type, or the minimum alignment usually required
15 by a type. Its syntax is just like ``sizeof`` and C11 ``_Alignof``.
16
17 For example, if the target machine requires a ``double`` value to be
18 aligned on an 8-byte boundary, then ``__alignof__ (double)`` is 8.
19 This is true on many RISC machines. On more traditional machine
20 designs, ``__alignof__ (double)`` is 4 or even 2.
21
22 Some machines never actually require alignment; they allow references to any
23 data type even at an odd address. For these machines, ``__alignof__``
24 reports the smallest alignment that GCC gives the data type, usually as
25 mandated by the target ABI.
26
27 If the operand of ``__alignof__`` is an lvalue rather than a type,
28 its value is the required alignment for its type, taking into account
29 any minimum alignment specified by attribute :var-attr:`aligned`
30 (see :ref:`common-variable-attributes`). For example, after this
31 declaration:
32
33 .. code-block:: c++
34
35 struct foo { int x; char y; } foo1;
36
37 the value of ``__alignof__ (foo1.y)`` is 1, even though its actual
38 alignment is probably 2 or 4, the same as ``__alignof__ (int)``.
39 It is an error to ask for the alignment of an incomplete type other
40 than ``void``.
41
42 If the operand of the ``__alignof__`` expression is a function,
43 the expression evaluates to the alignment of the function which may
44 be specified by attribute :fn-attr:`aligned` (see :ref:`common-function-attributes`).