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>Aliasing Type Rules (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Aliasing Type Rules (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Aliasing Type Rules (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="Aliasing.html" rel="up" title="Aliasing">
|
---|
33 | <link href="Digraphs.html" rel="next" title="Digraphs">
|
---|
34 | <link href="Aliasing-Length.html" rel="prev" title="Aliasing Length">
|
---|
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="Aliasing-Type-Rules"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Previous: <a href="Aliasing-Length.html" accesskey="p" rel="prev">Aliasing Length</a>, Up: <a href="Aliasing.html" accesskey="u" rel="up">Aliasing</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-Rules-for-Aliasing"></span><h3 class="appendixsection">B.3 Type Rules for Aliasing</h3>
|
---|
64 |
|
---|
65 | <p>C code that converts a pointer to a different pointer type can use the
|
---|
66 | pointers to access the same memory locations with two different data
|
---|
67 | types. If the same address is accessed with different types in a
|
---|
68 | single control thread, optimization can make the code do surprising
|
---|
69 | things (in effect, make it malfunction).
|
---|
70 | </p>
|
---|
71 | <p>Here’s a concrete example where aliasing that can change the code’s
|
---|
72 | behavior when it is optimized. We assume that <code>float</code> is 4 bytes
|
---|
73 | long, like <code>int</code>, and so is every pointer. Thus, the structures
|
---|
74 | <code>struct a</code> and <code>struct b</code> are both 8 bytes.
|
---|
75 | </p>
|
---|
76 | <div class="example">
|
---|
77 | <pre class="example">#include <stdio.h>
|
---|
78 | struct a { int size; char *data; };
|
---|
79 | struct b { float size; char *data; };
|
---|
80 |
|
---|
81 | void sub (struct a *p, struct b *q)
|
---|
82 | {
|
---|
83 | int x;
|
---|
84 | p->size = 0;
|
---|
85 | q->size = 1;
|
---|
86 | x = p->size;
|
---|
87 | printf("x =%d\n", x);
|
---|
88 | printf("p->size =%d\n", (int)p->size);
|
---|
89 | printf("q->size =%d\n", (int)q->size);
|
---|
90 | }
|
---|
91 |
|
---|
92 | int main(void)
|
---|
93 | {
|
---|
94 | struct a foo;
|
---|
95 | struct a *p = &foo;
|
---|
96 | struct b *q = (struct b *) &foo;
|
---|
97 |
|
---|
98 | sub (p, q);
|
---|
99 | }
|
---|
100 | </pre></div>
|
---|
101 |
|
---|
102 | <p>This code works as intended when compiled without optimization. All
|
---|
103 | the operations are carried out sequentially as written. The code
|
---|
104 | sets <code>x</code> to <code>p->size</code>, but what it actually gets is the
|
---|
105 | bits of the floating point number 1, as type <code>int</code>.
|
---|
106 | </p>
|
---|
107 | <p>However, when optimizing, the compiler is allowed to assume
|
---|
108 | (mistakenly, here) that <code>q</code> does not point to the same storage as
|
---|
109 | <code>p</code>, because their data types are not allowed to alias.
|
---|
110 | </p>
|
---|
111 | <p>From this assumption, the compiler can deduce (falsely, here) that the
|
---|
112 | assignment into <code>q->size</code> has no effect on the value of
|
---|
113 | <code>p->size</code>, which must therefore still be 0. Thus, <code>x</code> will
|
---|
114 | be set to 0.
|
---|
115 | </p>
|
---|
116 | <p>GNU C, following the C standard, <em>defines</em> this optimization as
|
---|
117 | legitimate. Code that misbehaves when optimized following these rules
|
---|
118 | is, by definition, incorrect C code.
|
---|
119 | </p>
|
---|
120 | <p>The rules for storage aliasing in C are based on the two data types:
|
---|
121 | the type of the object, and the type it is accessed through. The
|
---|
122 | rules permit accessing part of a storage object of type <var>t</var> using
|
---|
123 | only these types:
|
---|
124 | </p>
|
---|
125 | <ul>
|
---|
126 | <li> <var>t</var>.
|
---|
127 |
|
---|
128 | </li><li> A type compatible with <var>t</var>. See <a href="Compatible-Types.html">Compatible Types</a>.
|
---|
129 |
|
---|
130 | </li><li> A signed or unsigned version of one of the above.
|
---|
131 |
|
---|
132 | </li><li> A qualifed version of one of the above.
|
---|
133 | See <a href="Type-Qualifiers.html">Type Qualifiers</a>.
|
---|
134 |
|
---|
135 | </li><li> An array, structure (see <a href="Structures.html">Structures</a>), or union type
|
---|
136 | (<code>Unions</code>) that contains one of the above, either directly as a
|
---|
137 | field or through multiple levels of fields. If <var>t</var> is
|
---|
138 | <code>double</code>, this would include <code>struct s { union { double
|
---|
139 | d[2]; int i[4]; } u; int i; };</code> because there’s a <code>double</code>
|
---|
140 | inside it somewhere.
|
---|
141 |
|
---|
142 | </li><li> A character type.
|
---|
143 | </li></ul>
|
---|
144 |
|
---|
145 | <p>What do these rules say about the example in this subsection?
|
---|
146 | </p>
|
---|
147 | <p>For <code>foo.size</code> (equivalently, <code>a->size</code>), <var>t</var> is
|
---|
148 | <code>int</code>. The type <code>float</code> is not allowed as an aliasing type
|
---|
149 | by those rules, so <code>b->size</code> is not supposed to alias with
|
---|
150 | elements of <code>j</code>. Based on that assumption, GNU C makes a
|
---|
151 | permitted optimization that was not, in this case, consistent with
|
---|
152 | what the programmer intended the program to do.
|
---|
153 | </p>
|
---|
154 | <p>Whether GCC actually performs type-based aliasing analysis depends on
|
---|
155 | the details of the code. GCC has other ways to determine (in some cases)
|
---|
156 | whether objects alias, and if it gets a reliable answer that way, it won’t
|
---|
157 | fall back on type-based heuristics.
|
---|
158 | </p>
|
---|
159 | <p>The importance of knowing the type-based aliasing rules is not so as
|
---|
160 | to ensure that the optimization is done where it would be safe, but so
|
---|
161 | as to ensure it is <em>not</em> done in a way that would break the
|
---|
162 | program. You can turn off type-based aliasing analysis by giving GCC
|
---|
163 | the option <samp>-fno-strict-aliasing</samp>.
|
---|
164 | </p>
|
---|
165 | <hr>
|
---|
166 | <div class="header">
|
---|
167 | <p>
|
---|
168 | Previous: <a href="Aliasing-Length.html" accesskey="p" rel="prev">Aliasing Length</a>, Up: <a href="Aliasing.html" accesskey="u" rel="up">Aliasing</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>
|
---|
169 | </div>
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 | </body>
|
---|
174 | </html>
|
---|