1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>11.2 Freeing Memory
</title>
10 <link href=
"sx11a.html" rev=precedes
>
11 <link href=
"sx11c.html" rel=precedes
>
12 <link href=
"sx11.html" rev=subdocument
>
15 <H2>11.2 Freeing Memory
</H2>
17 <p>Memory allocated with
<TT>malloc
</TT>
18 lasts as long as you want it to.
19 It does not automatically disappear when a function returns,
20 as automatic-duration variables do,
21 but it does not have to remain
22 for the entire duration of your program,
24 Just as you can use
<TT>malloc
</TT> to control
25 exactly when and how much memory you allocate,
26 you can also control exactly when you deallocate it.
28 many programs use memory on a transient basis.
29 They allocate some memory, use it for a while,
30 but then reach a point where they don't need that particular piece any more.
31 Because memory is not inexhaustible,
36 release or
<dfn>free
</dfn>)
37 memory you're no longer using.
38 </p><p>Dynamically allocated memory is deallocated with
39 the
<TT>free
</TT> function.
40 If
<TT>p
</TT> contains a pointer previously returned by
<TT>malloc
</TT>,
45 which will ``give the memory back'' to the stock of memory
46 (sometimes called the ``arena'' or ``pool'')
47 from which
<TT>malloc
</TT> requests are satisfied.
48 Calling
<TT>free
</TT> is sort of the ultimate in recycling:
49 it costs you almost nothing,
50 and the memory you give back is immediately usable
51 by other parts of your program.
52 (Theoretically, it may even be usable by other programs.)
53 </p><p>(Freeing unused memory is a good idea,
54 but it's not mandatory.
55 When your program exits,
56 any memory which it has allocated but not freed
57 should be automatically released.
58 If your computer were to somehow ``lose'' memory
59 just because your program forgot to free it,
60 that would indicate a problem or deficiency in your operating system.)
62 once you've freed some memory
63 you must remember not to use it any more.
68 it is probably the case
70 still points at the same memory.
71 However, since we've given it back,
72 it's now ``available,''
74 a later call to
<TT>malloc
</TT> might give that memory
75 to some other part of your program.
76 If the variable
<TT>p
</TT> is a global variable
77 or will otherwise stick around for a while,
78 one good way to record the fact that it's not to be used any more
79 would be to set it to a null pointer:
84 Now we don't even have the pointer to the freed memory any more,
86 (as long as we check to see that
<TT>p
</TT> is non-
<TT>NULL
</TT>
88 we won't misuse any memory via the pointer
<TT>p
</TT>.
89 </p><p>When thinking about
<TT>malloc
</TT>,
91 and dynamically-allocated memory in general,
92 remember again the distinction between a pointer and what it points to.
93 If you call
<TT>malloc
</TT> to allocate some memory,
94 and store the pointer which
<TT>malloc
</TT> gives you in a
95 local pointer variable,
96 what happens when the function containing the local pointer
98 If the local pointer variable has
<dfn>automatic duration
</dfn>
99 (which is the default,
100 unless the variable is declared
<TT>static
</TT>),
101 it will disappear when the function returns.
104 the pointer variable to disappear says nothing about
105 the memory pointed to!
106 That memory still exists and,
107 as far as
<TT>malloc
</TT> and
<TT>free
</TT> are concerned,
109 The only thing that has disappeared
110 is the pointer variable you had
111 which pointed at the allocated memory.
113 if it contained the only copy of the pointer you had,
115 you'll have no way of freeing the memory,
116 and no way of using it, either.
120 require that you have at least one pointer to the memory!)
124 <a href=
"sx11a.html" rev=precedes
>prev
</a>
125 <a href=
"sx11c.html" rel=precedes
>next
</a>
126 <a href=
"sx11.html" rev=subdocument
>up
</a>
127 <a href=
"top.html">top
</a>
130 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
131 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
132 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>