source: public/doc/gnu-c/Dynamic-Memory-Allocation.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: 6.1 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>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<!--
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="Dynamic-Memory-Allocation"></span><div class="header">
59<p>
60Next: <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> &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="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
70Reference 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 &lt;stddef.h&gt; /* <span class="roman">Defines <code>NULL</code>.</span> */
76#include &lt;stdlib.h&gt; /* <span class="roman">Declares <code>malloc</code>.</span> */
77
78&hellip;
79
80struct intlistlink *
81alloc_intlistlink ()
82{
83 struct intlistlink *p;
84
85 p = malloc (sizeof (struct intlistlink));
86
87 if (p == NULL)
88 fatal (&quot;Ran out of storage&quot;);
89
90 /* <span class="roman">Initialize the contents.</span> */
91 p-&gt;datum = 0;
92 p-&gt;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>
99will automatically convert it to type <code>struct intlistlink *</code>.
100The 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>
104returns a null pointer if it cannot get any storage. We assume that
105the program defines the function <code>fatal</code> to report a fatal error
106to the user.
107</p>
108<p>Here&rsquo;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
113void
114add_to_mylist (int my_int)
115{
116 struct intlistlink *p = alloc_intlistlink ();
117
118 p-&gt;datum = my_int;
119 p-&gt;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&rsquo;s
125a function to free all the links in one of these lists:
126</p>
127<div class="example">
128<pre class="example">void
129free_intlist (struct intlistlink *p)
130{
131 while (p)
132 {
133 struct intlistlink *q = p;
134 p = p-&gt;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
141it, because <code>free</code> can clobber the data that was in the object.
142For the same reason, the program must not use the list any more after
143freeing its elements. To make sure it won&rsquo;t, it is best to clear out
144the variable where the list was stored, like this:
145</p>
146<div class="example">
147<pre class="example">free_intlist (mylist);
148
149mylist = NULL;
150</pre></div>
151
152<hr>
153<div class="header">
154<p>
155Next: <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> &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>
156</div>
157
158
159
160</body>
161</html>
Note: See TracBrowser for help on using the repository browser.