1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Type Alignment (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Type Alignment (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Type Alignment (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="index.html" rel="up" title="Top">
|
---|
33 | <link href="Aliasing.html" rel="next" title="Aliasing">
|
---|
34 | <link href="Static-Assertions.html" rel="prev" title="Static Assertions">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Type-Alignment"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Aliasing.html" accesskey="n" rel="next">Aliasing</a>, Previous: <a href="Directing-Compilation.html" accesskey="p" rel="prev">Directing Compilation</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Type-Alignment-1"></span><h2 class="appendix">Appendix A Type Alignment</h2>
|
---|
64 | <span id="index-type-alignment"></span>
|
---|
65 | <span id="index-alignment-of-type"></span>
|
---|
66 | <span id="index-_005fAlignof"></span>
|
---|
67 | <span id="index-_005f_005falignof_005f_005f"></span>
|
---|
68 |
|
---|
69 | <p>Code for device drivers and other communication with low-level
|
---|
70 | hardware sometimes needs to be concerned with the alignment of
|
---|
71 | data objects in memory.
|
---|
72 | </p>
|
---|
73 | <p>Each data type has a required <em>alignment</em>, always a power of 2,
|
---|
74 | that says at which memory addresses an object of that type can validly
|
---|
75 | start. A valid address for the type must be a multiple of its
|
---|
76 | alignment. If a type’s alignment is 1, that means it can validly
|
---|
77 | start at any address. If a type’s alignment is 2, that means it can
|
---|
78 | only start at an even address. If a type’s alignment is 4, that means
|
---|
79 | it can only start at an address that is a multiple of 4.
|
---|
80 | </p>
|
---|
81 | <p>The alignment of a type (except <code>char</code>) can vary depending on the
|
---|
82 | kind of computer in use. To refer to the alignment of a type in a C
|
---|
83 | program, use <code>_Alignof</code>, whose syntax parallels that of
|
---|
84 | <code>sizeof</code>. Like <code>sizeof</code>, <code>_Alignof</code> is a compile-time
|
---|
85 | operation, and it doesn’t compute the value of the expression used
|
---|
86 | as its argument.
|
---|
87 | </p>
|
---|
88 | <p>Nominally, each integer and floating-point type has an alignment equal to
|
---|
89 | the largest power of 2 that divides its size. Thus, <code>int</code> with
|
---|
90 | size 4 has a nominal alignment of 4, and <code>long long int</code> with
|
---|
91 | size 8 has a nominal alignment of 8.
|
---|
92 | </p>
|
---|
93 | <p>However, each kind of computer generally has a maximum alignment, and
|
---|
94 | no type needs more alignment than that. If the computer’s maximum
|
---|
95 | alignment is 4 (which is common), then no type’s alignment is more
|
---|
96 | than 4.
|
---|
97 | </p>
|
---|
98 | <p>The size of any type is always a multiple of its alignment; that way,
|
---|
99 | in an array whose elements have that type, all the elements are
|
---|
100 | properly aligned if the first one is.
|
---|
101 | </p>
|
---|
102 | <p>These rules apply to all real computers today, but some embedded
|
---|
103 | controllers have odd exceptions. We don’t have references to cite for
|
---|
104 | them.
|
---|
105 | </p>
|
---|
106 | <p>Ordinary C code guarantees that every object of a given type is in
|
---|
107 | fact aligned as that type requires.
|
---|
108 | </p>
|
---|
109 | <p>If the operand of <code>_Alignof</code> is a structure field, the value
|
---|
110 | is the alignment it requires. It may have a greater alignment by
|
---|
111 | coincidence, due to the other fields, but <code>_Alignof</code> is not
|
---|
112 | concerned about that. See <a href="Structures.html">Structures</a>.
|
---|
113 | </p>
|
---|
114 | <p>Older versions of GNU C used the keyword <code>__alignof__</code> for this,
|
---|
115 | but now that the feature has been standardized, it is better
|
---|
116 | to use the standard keyword <code>_Alignof</code>.
|
---|
117 | </p>
|
---|
118 | <span id="index-_005fAlignas"></span>
|
---|
119 | <span id="index-_005f_005faligned_005f_005f"></span>
|
---|
120 | <p>You can explicitly specify an alignment requirement for a particular
|
---|
121 | variable or structure field by adding <code>_Alignas
|
---|
122 | (<var>alignment</var>)</code> to the declaration, where <var>alignment</var> is a
|
---|
123 | power of 2 or a type name. For instance:
|
---|
124 | </p>
|
---|
125 | <div class="example">
|
---|
126 | <pre class="example">char _Alignas (8) x;
|
---|
127 | </pre></div>
|
---|
128 |
|
---|
129 | <p>or
|
---|
130 | </p>
|
---|
131 | <div class="example">
|
---|
132 | <pre class="example">char _Alignas (double) x;
|
---|
133 | </pre></div>
|
---|
134 |
|
---|
135 | <p>specifies that <code>x</code> must start on an address that is a multiple of
|
---|
136 | 8. However, if <var>alignment</var> exceeds the maximum alignment for the
|
---|
137 | machine, that maximum is how much alignment <code>x</code> will get.
|
---|
138 | </p>
|
---|
139 | <p>The older GNU C syntax for this feature looked like
|
---|
140 | <code>__attribute__ ((__aligned__ (<var>alignment</var>)))</code> to the
|
---|
141 | declaration, and was added after the variable. For instance:
|
---|
142 | </p>
|
---|
143 | <div class="example">
|
---|
144 | <pre class="example">char x __attribute__ ((__aligned__ 8));
|
---|
145 | </pre></div>
|
---|
146 |
|
---|
147 | <p>See <a href="Attributes.html">Attributes</a>.
|
---|
148 | </p>
|
---|
149 | <hr>
|
---|
150 | <div class="header">
|
---|
151 | <p>
|
---|
152 | Next: <a href="Aliasing.html" accesskey="n" rel="next">Aliasing</a>, Previous: <a href="Directing-Compilation.html" accesskey="p" rel="prev">Directing Compilation</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
153 | </div>
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 | </body>
|
---|
158 | </html>
|
---|