source: public/doc/gnu-c/Structure-Layout.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: 5.8 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>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<!--
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="Structure-Layout"></span><div class="header">
59<p>
60Next: <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> &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="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
68you 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
71overlay hardware registers, to access data structures in shared
72memory, or to assemble and disassemble packets for network
73communication. It is also important for avoiding memory waste when
74the program makes many objects of that type. However, the layout
75depends on the target platform. Each platform has conventions for
76structure layout, which compilers need to follow.
77</p>
78<p>Here are the conventions used on most platforms.
79</p>
80<p>The structure&rsquo;s fields appear in the structure layout in the order
81they are declared. When possible, consecutive fields occupy
82consecutive bytes within the structure. However, if a field&rsquo;s type
83demands more alignment than it would get that way, C gives it the
84alignment 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
87the structure&rsquo;s alignment and size. The structure&rsquo;s alignment is the
88maximum alignment of any of the fields in it. Then the structure&rsquo;s
89size is rounded up to a multiple of its alignment. That may require
90leaving 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
93alignment 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
105at offset 0, <code>b</code> is at offset 1, and <code>c</code> is at offset 4.
106There 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
121gaps: 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
124takes 8 bytes and the other takes 12 bytes due to the ordering of the
125fields. A reliable way to avoid this sort of wastage is to order the
126fields by size, biggest fields first.
127</p>
128<hr>
129<div class="header">
130<p>
131Next: <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> &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>
132</div>
133
134
135
136</body>
137</html>
Note: See TracBrowser for help on using the repository browser.