* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx11d.html
blobdc0c2e26dcc2a4adfcb475ff36726ec56172394f
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. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>11.4 Pointer Safety</title>
10 <link href="sx11c.html" rev=precedes>
11 <link href="sx12.html" rel=precedes>
12 <link href="sx11.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>11.4 Pointer Safety</H2>
17 <p>At the beginning of the previous chapter,
18 we said that the hard thing about pointers
19 is not so much manipulating them
20 as ensuring that the memory they point to is valid.
21 When a pointer doesn't point where you think it does,
22 if you inadvertently access or modify the memory it points to,
23 you can damage other parts of your program,
24 or (in some cases) other programs or the operating system itself!
25 </p><p>When we use pointers to simple variables,
26 as in section
28 10.1,
29 there's not much that can go wrong.
30 When we use pointers into arrays,
31 as in section
33 10.2,
34 and begin moving the pointers around,
35 we have to be more careful,
36 to ensure that the roving pointers
37 always stay within the bounds of the array(s).
38 When we begin passing pointers to functions,
39 and especially when we begin returning them from functions
40 (as in the <TT>strstr</TT> function of section
42 10.4)
43 we have to be more careful still,
44 because the code using the pointer may be far removed
45 from the code which owns or allocated
47 the memory.
48 </p><p>One particular problem concerns functions that return pointers.
49 Where is the memory to which the returned pointer points?
50 Is it still around by the time the function returns?
51 The <TT>strstr</TT> function returns either a null pointer
52 (which points definitively nowhere,
53 and which the caller presumably checks for)
54 or it returns a pointer which points into the input string,
55 which the caller supplied, which is pretty safe.
56 One thing a function must <em>not</em> do,
57 however,
58 is return a pointer to one of its own, local, automatic-duration arrays.
59 Remember that
60 automatic-duration variables
61 (which includes all non-static local variables),
62 including automatic-duration arrays,
63 are deallocated and disappear when the function returns.
64 If a function returns a pointer to a local array,
65 that pointer will be invalid by the time the caller tries to use it.
66 </p><p>Finally,
68 when we're doing dynamic memory allocation
69 with <TT>malloc</TT>, <TT>realloc</TT>, and <TT>free</TT>,
70 we have to be most careful of all.
71 Dynamic allocation
72 gives us a lot more flexibility in how our programs use memory,
73 although with that flexibility comes the responsibility
74 that we manage dynamically allocated memory carefully.
75 The possibilities for misdirected pointers and associated mayhem
76 are greatest in programs that make heavy use of dynamic memory allocation.
77 You can reduce these possibilities by designing your program
79 in such a way that it's easy to ensure
80 that pointers are used correctly
81 and that memory is always allocated and deallocated correctly.
83 (If, on the other hand,
84 your program is designed in such a way
85 that meeting these guarantees is a tedious nuisance,
86 sooner or later you'll forget or neglect to,
87 and maintenance will be a nightmare.)
88 </p><hr>
89 <p>
90 Read sequentially:
91 <a href="sx11c.html" rev=precedes>prev</a>
92 <a href="sx12.html" rel=precedes>next</a>
93 <a href="sx11.html" rev=subdocument>up</a>
94 <a href="top.html">top</a>
95 </p>
96 <p>
97 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
98 // <a href="copyright.html">Copyright</a> 1995, 1996
99 // <a href="mailto:scs@eskimo.com">mail feedback</a>
100 </p>
101 </body>
102 </html>