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>Structure Layout (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Structure Layout (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Structure Layout (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="Structures.html" rel="up" title="Structures">
|
---|
33 | <link href="Packed-Structures.html" rel="next" title="Packed Structures">
|
---|
34 | <link href="Field-Offset.html" rel="prev" title="Field Offset">
|
---|
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="Structure-Layout"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Packed-Structures.html" accesskey="n" rel="next">Packed Structures</a>, Previous: <a href="Field-Offset.html" accesskey="p" rel="prev">Field Offset</a>, Up: <a href="Structures.html" accesskey="u" rel="up">Structures</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="Structure-Layout-1"></span><h3 class="section">15.4 Structure Layout</h3>
|
---|
64 | <span id="index-structure-layout"></span>
|
---|
65 | <span id="index-layout-of-structures"></span>
|
---|
66 |
|
---|
67 | <p>The rest of this chapter covers advanced topics about structures. If
|
---|
68 | you are just learning C, you can skip it.
|
---|
69 | </p>
|
---|
70 | <p>The precise layout of a <code>struct</code> type is crucial when using it to
|
---|
71 | overlay hardware registers, to access data structures in shared
|
---|
72 | memory, or to assemble and disassemble packets for network
|
---|
73 | communication. It is also important for avoiding memory waste when
|
---|
74 | the program makes many objects of that type. However, the layout
|
---|
75 | depends on the target platform. Each platform has conventions for
|
---|
76 | structure layout, which compilers need to follow.
|
---|
77 | </p>
|
---|
78 | <p>Here are the conventions used on most platforms.
|
---|
79 | </p>
|
---|
80 | <p>The structure’s fields appear in the structure layout in the order
|
---|
81 | they are declared. When possible, consecutive fields occupy
|
---|
82 | consecutive bytes within the structure. However, if a field’s type
|
---|
83 | demands more alignment than it would get that way, C gives it the
|
---|
84 | alignment it requires by leaving a gap after the previous field.
|
---|
85 | </p>
|
---|
86 | <p>Once all the fields have been laid out, it is possible to determine
|
---|
87 | the structure’s alignment and size. The structure’s alignment is the
|
---|
88 | maximum alignment of any of the fields in it. Then the structure’s
|
---|
89 | size is rounded up to a multiple of its alignment. That may require
|
---|
90 | leaving a gap at the end of the structure.
|
---|
91 | </p>
|
---|
92 | <p>Here are some examples, where we assume that <code>char</code> has size and
|
---|
93 | alignment 1 (always true), and <code>int</code> has size and alignment 4
|
---|
94 | (true on most kinds of computers):
|
---|
95 | </p>
|
---|
96 | <div class="example">
|
---|
97 | <pre class="example">struct foo
|
---|
98 | {
|
---|
99 | char a, b;
|
---|
100 | int c;
|
---|
101 | };
|
---|
102 | </pre></div>
|
---|
103 |
|
---|
104 | <p>This structure occupies 8 bytes, with an alignment of 4. <code>a</code> is
|
---|
105 | at offset 0, <code>b</code> is at offset 1, and <code>c</code> is at offset 4.
|
---|
106 | There is a gap of 2 bytes before <code>c</code>.
|
---|
107 | </p>
|
---|
108 | <p>Contrast that with this structure:
|
---|
109 | </p>
|
---|
110 | <div class="example">
|
---|
111 | <pre class="example">struct foo
|
---|
112 | {
|
---|
113 | char a;
|
---|
114 | int c;
|
---|
115 | char b;
|
---|
116 | };
|
---|
117 | </pre></div>
|
---|
118 |
|
---|
119 | <p>This structure has size 12 and alignment 4. <code>a</code> is at offset 0,
|
---|
120 | <code>c</code> is at offset 4, and <code>b</code> is at offset 8. There are two
|
---|
121 | gaps: three bytes before <code>c</code>, and three bytes at the end.
|
---|
122 | </p>
|
---|
123 | <p>These two structures have the same contents at the C level, but one
|
---|
124 | takes 8 bytes and the other takes 12 bytes due to the ordering of the
|
---|
125 | fields. A reliable way to avoid this sort of wastage is to order the
|
---|
126 | fields by size, biggest fields first.
|
---|
127 | </p>
|
---|
128 | <hr>
|
---|
129 | <div class="header">
|
---|
130 | <p>
|
---|
131 | Next: <a href="Packed-Structures.html" accesskey="n" rel="next">Packed Structures</a>, Previous: <a href="Field-Offset.html" accesskey="p" rel="prev">Field Offset</a>, Up: <a href="Structures.html" accesskey="u" rel="up">Structures</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>
|
---|
132 | </div>
|
---|
133 |
|
---|
134 |
|
---|
135 |
|
---|
136 | </body>
|
---|
137 | </html>
|
---|