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>Dynamic Memory Allocation (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Dynamic Memory Allocation (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Dynamic Memory Allocation (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="Field-Offset.html" rel="next" title="Field Offset">
|
---|
34 | <link href="Referencing-Fields.html" rel="prev" title="Referencing Fields">
|
---|
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="Dynamic-Memory-Allocation"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Field-Offset.html" accesskey="n" rel="next">Field Offset</a>, Previous: <a href="Referencing-Fields.html" accesskey="p" rel="prev">Referencing Fields</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="Dynamic-Memory-Allocation-1"></span><h3 class="section">15.2 Dynamic Memory Allocation</h3>
|
---|
64 | <span id="index-dynamic-memory-allocation"></span>
|
---|
65 | <span id="index-memory-allocation_002c-dynamic"></span>
|
---|
66 | <span id="index-allocating-memory-dynamically"></span>
|
---|
67 |
|
---|
68 | <p>To allocate an object dynamically, call the library function
|
---|
69 | <code>malloc</code> (see <a href="https://www.gnu.org/software/libc/manual/html_node/Basic-Allocation.html#Basic-Allocation">The GNU C Library</a> in <cite>The GNU C Library
|
---|
70 | Reference Manual</cite>). Here is how to allocate an object of type
|
---|
71 | <code>struct intlistlink</code>. To make this code work, include the file
|
---|
72 | <samp>stdlib.h</samp>, like this:
|
---|
73 | </p>
|
---|
74 | <div class="example">
|
---|
75 | <pre class="example">#include <stddef.h> /* <span class="roman">Defines <code>NULL</code>.</span> */
|
---|
76 | #include <stdlib.h> /* <span class="roman">Declares <code>malloc</code>.</span> */
|
---|
77 |
|
---|
78 | …
|
---|
79 |
|
---|
80 | struct intlistlink *
|
---|
81 | alloc_intlistlink ()
|
---|
82 | {
|
---|
83 | struct intlistlink *p;
|
---|
84 |
|
---|
85 | p = malloc (sizeof (struct intlistlink));
|
---|
86 |
|
---|
87 | if (p == NULL)
|
---|
88 | fatal ("Ran out of storage");
|
---|
89 |
|
---|
90 | /* <span class="roman">Initialize the contents.</span> */
|
---|
91 | p->datum = 0;
|
---|
92 | p->next = NULL;
|
---|
93 |
|
---|
94 | return p;
|
---|
95 | }
|
---|
96 | </pre></div>
|
---|
97 |
|
---|
98 | <p><code>malloc</code> returns <code>void *</code>, so the assignment to <code>p</code>
|
---|
99 | will automatically convert it to type <code>struct intlistlink *</code>.
|
---|
100 | The return value of <code>malloc</code> is always sufficiently aligned
|
---|
101 | (see <a href="Type-Alignment.html">Type Alignment</a>) that it is valid for any data type.
|
---|
102 | </p>
|
---|
103 | <p>The test for <code>p == NULL</code> is necessary because <code>malloc</code>
|
---|
104 | returns a null pointer if it cannot get any storage. We assume that
|
---|
105 | the program defines the function <code>fatal</code> to report a fatal error
|
---|
106 | to the user.
|
---|
107 | </p>
|
---|
108 | <p>Here’s how to add one more integer to the front of such a list:
|
---|
109 | </p>
|
---|
110 | <div class="example">
|
---|
111 | <pre class="example">struct intlistlink *my_list = NULL;
|
---|
112 |
|
---|
113 | void
|
---|
114 | add_to_mylist (int my_int)
|
---|
115 | {
|
---|
116 | struct intlistlink *p = alloc_intlistlink ();
|
---|
117 |
|
---|
118 | p->datum = my_int;
|
---|
119 | p->next = mylist;
|
---|
120 | mylist = p;
|
---|
121 | }
|
---|
122 | </pre></div>
|
---|
123 |
|
---|
124 | <p>The way to free the objects is by calling <code>free</code>. Here’s
|
---|
125 | a function to free all the links in one of these lists:
|
---|
126 | </p>
|
---|
127 | <div class="example">
|
---|
128 | <pre class="example">void
|
---|
129 | free_intlist (struct intlistlink *p)
|
---|
130 | {
|
---|
131 | while (p)
|
---|
132 | {
|
---|
133 | struct intlistlink *q = p;
|
---|
134 | p = p->next;
|
---|
135 | free (q);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | </pre></div>
|
---|
139 |
|
---|
140 | <p>We must extract the <code>next</code> pointer from the object before freeing
|
---|
141 | it, because <code>free</code> can clobber the data that was in the object.
|
---|
142 | For the same reason, the program must not use the list any more after
|
---|
143 | freeing its elements. To make sure it won’t, it is best to clear out
|
---|
144 | the variable where the list was stored, like this:
|
---|
145 | </p>
|
---|
146 | <div class="example">
|
---|
147 | <pre class="example">free_intlist (mylist);
|
---|
148 |
|
---|
149 | mylist = NULL;
|
---|
150 | </pre></div>
|
---|
151 |
|
---|
152 | <hr>
|
---|
153 | <div class="header">
|
---|
154 | <p>
|
---|
155 | Next: <a href="Field-Offset.html" accesskey="n" rel="next">Field Offset</a>, Previous: <a href="Referencing-Fields.html" accesskey="p" rel="prev">Referencing Fields</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>
|
---|
156 | </div>
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 | </body>
|
---|
161 | </html>
|
---|