source: public/doc/gnu-c/Type-Alignment.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.0 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
70hardware sometimes needs to be concerned with the alignment of
71data objects in memory.
72</p>
73<p>Each data type has a required <em>alignment</em>, always a power of 2,
74that says at which memory addresses an object of that type can validly
75start. A valid address for the type must be a multiple of its
76alignment. If a type&rsquo;s alignment is 1, that means it can validly
77start at any address. If a type&rsquo;s alignment is 2, that means it can
78only start at an even address. If a type&rsquo;s alignment is 4, that means
79it 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
82kind of computer in use. To refer to the alignment of a type in a C
83program, use <code>_Alignof</code>, whose syntax parallels that of
84<code>sizeof</code>. Like <code>sizeof</code>, <code>_Alignof</code> is a compile-time
85operation, and it doesn&rsquo;t compute the value of the expression used
86as its argument.
87</p>
88<p>Nominally, each integer and floating-point type has an alignment equal to
89the largest power of 2 that divides its size. Thus, <code>int</code> with
90size 4 has a nominal alignment of 4, and <code>long long int</code> with
91size 8 has a nominal alignment of 8.
92</p>
93<p>However, each kind of computer generally has a maximum alignment, and
94no type needs more alignment than that. If the computer&rsquo;s maximum
95alignment is 4 (which is common), then no type&rsquo;s alignment is more
96than 4.
97</p>
98<p>The size of any type is always a multiple of its alignment; that way,
99in an array whose elements have that type, all the elements are
100properly aligned if the first one is.
101</p>
102<p>These rules apply to all real computers today, but some embedded
103controllers have odd exceptions. We don&rsquo;t have references to cite for
104them.
105</p>
106<p>Ordinary C code guarantees that every object of a given type is in
107fact aligned as that type requires.
108</p>
109<p>If the operand of <code>_Alignof</code> is a structure field, the value
110is the alignment it requires. It may have a greater alignment by
111coincidence, due to the other fields, but <code>_Alignof</code> is not
112concerned 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,
115but now that the feature has been standardized, it is better
116to 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
121variable or structure field by adding <code>_Alignas
122(<var>alignment</var>)</code> to the declaration, where <var>alignment</var> is a
123power 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
1368. However, if <var>alignment</var> exceeds the maximum alignment for the
137machine, 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
141declaration, 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>
152Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.