1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 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>Chapter
11: Memory Allocation
</title>
10 <link href=
"sx10h.html" rev=precedes
>
11 <link href=
"sx11a.html" rel=precedes
>
12 <link href=
"top.html" rev=subdocument
>
15 <H1>Chapter
11: Memory Allocation
</H1>
17 <p>In this chapter, we'll meet
<TT>malloc
</TT>,
18 C's dynamic memory allocation function,
19 and we'll cover dynamic memory allocation in some detail.
20 </p><p>As we begin doing dynamic memory allocation, we'll begin to see
21 (if we haven't seen it already)
22 what pointers can really be good for.
23 Many of the pointer examples in the previous chapter
24 (those which used pointers to access arrays)
25 didn't do all that much for us
26 that we couldn't have done
29 when we begin doing dynamic memory allocation,
30 pointers are the only way to go,
31 because what
<TT>malloc
</TT> returns
32 is a pointer to the memory it gives us.
33 (Due to the equivalence between pointers and arrays,
35 we will still be able to think of
36 dynamically allocated regions of storage as if they were arrays,
37 and even to use array-like subscripting notation on them.)
38 </p><p>You have to be careful with dynamic memory allocation.
39 <TT>malloc
</TT> operates at a pretty ``low level'';
40 you will often find yourself having to do a certain amount of
42 to manage the memory it gives you.
43 If you don't keep accurate track
44 of the memory which
<TT>malloc
</TT> has given you,
45 and the pointers of yours which point to it,
46 it's all too easy to accidentally use a pointer
47 which points ``nowhere'',
48 with generally unpleasant results.
51 is that if you assign a value to the location pointed to by a pointer:
55 and if the pointer
<TT>p
</TT> points ``nowhere'',
57 well actually it can be construed to point somewhere,
58 just not where you wanted it to,
59 and that ``somewhere'' is where the
0 gets written.
60 If the ``somewhere'' is memory
61 which is in use by some other part of your program,
63 if the operating system has not protected itself from you
64 and ``somewhere'' is in fact in use by the operating system,
65 things could get ugly.)
67 </p><p><a href=
"sx11a.html" rel=subdocument
>11.1 Allocating Memory with
<TT>malloc
</TT></a></p>
68 <p><a href=
"sx11b.html" rel=subdocument
>11.2 Freeing Memory
</a></p>
69 <p><a href=
"sx11c.html" rel=subdocument
>11.3 Reallocating Memory Blocks
</a></p>
70 <p><a href=
"sx11d.html" rel=subdocument
>11.4 Pointer Safety
</a></p>
74 <a href=
"sx10h.html" rev=precedes
>prev
</a>
75 <a href=
"sx11a.html" rel=precedes
>next
</a>
76 <a href=
"top.html" rev=subdocument
>up
</a>
77 <a href=
"top.html">top
</a>
80 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
81 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
82 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>