| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | .\" $NetBSD: malloc.3,v 1.29 2009/05/18 09:00:02 wiz Exp $ | | 1 | .\" $NetBSD: malloc.3,v 1.30 2009/07/20 12:10:03 pooka Exp $ |
2 | .\" | | 2 | .\" |
3 | .\" Copyright (c) 1980, 1991, 1993 | | 3 | .\" Copyright (c) 1980, 1991, 1993 |
4 | .\" The Regents of the University of California. All rights reserved. | | 4 | .\" The Regents of the University of California. All rights reserved. |
5 | .\" | | 5 | .\" |
6 | .\" This code is derived from software contributed to Berkeley by | | 6 | .\" This code is derived from software contributed to Berkeley by |
7 | .\" the American National Standards Committee X3, on Information | | 7 | .\" the American National Standards Committee X3, on Information |
8 | .\" Processing Systems. | | 8 | .\" Processing Systems. |
9 | .\" | | 9 | .\" |
10 | .\" Redistribution and use in source and binary forms, with or without | | 10 | .\" Redistribution and use in source and binary forms, with or without |
11 | .\" modification, are permitted provided that the following conditions | | 11 | .\" modification, are permitted provided that the following conditions |
12 | .\" are met: | | 12 | .\" are met: |
13 | .\" 1. Redistributions of source code must retain the above copyright | | 13 | .\" 1. Redistributions of source code must retain the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer. | | 14 | .\" notice, this list of conditions and the following disclaimer. |
| @@ -24,27 +24,27 @@ | | | @@ -24,27 +24,27 @@ |
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | .\" SUCH DAMAGE. | | 32 | .\" SUCH DAMAGE. |
33 | .\" | | 33 | .\" |
34 | .\" @(#)malloc.3 8.1 (Berkeley) 6/4/93 | | 34 | .\" @(#)malloc.3 8.1 (Berkeley) 6/4/93 |
35 | .\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.73 2007/06/15 22:32:33 jasone Exp $ | | 35 | .\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.73 2007/06/15 22:32:33 jasone Exp $ |
36 | .\" | | 36 | .\" |
37 | .Dd October 15, 2007 | | 37 | .Dd June 20, 2009 |
38 | .Dt MALLOC 3 | | 38 | .Dt MALLOC 3 |
39 | .Os | | 39 | .Os |
40 | .Sh NAME | | 40 | .Sh NAME |
41 | .Nm malloc , calloc , realloc , free | | 41 | .Nm malloc , calloc , realloc , free |
42 | .Nd general purpose memory allocation functions | | 42 | .Nd general purpose memory allocation functions |
43 | .Sh LIBRARY | | 43 | .Sh LIBRARY |
44 | .Lb libc | | 44 | .Lb libc |
45 | .Sh SYNOPSIS | | 45 | .Sh SYNOPSIS |
46 | .In stdlib.h | | 46 | .In stdlib.h |
47 | .Ft void * | | 47 | .Ft void * |
48 | .Fn malloc "size_t size" | | 48 | .Fn malloc "size_t size" |
49 | .Ft void * | | 49 | .Ft void * |
50 | .Fn calloc "size_t number" "size_t size" | | 50 | .Fn calloc "size_t number" "size_t size" |
| @@ -96,26 +96,56 @@ Note that | | | @@ -96,26 +96,56 @@ Note that |
96 | .Fn realloc | | 96 | .Fn realloc |
97 | may move the memory allocation, resulting in a different return value than | | 97 | may move the memory allocation, resulting in a different return value than |
98 | .Fa ptr . | | 98 | .Fa ptr . |
99 | If | | 99 | If |
100 | .Fa ptr | | 100 | .Fa ptr |
101 | is | | 101 | is |
102 | .Dv NULL , | | 102 | .Dv NULL , |
103 | the | | 103 | the |
104 | .Fn realloc | | 104 | .Fn realloc |
105 | function behaves identically to | | 105 | function behaves identically to |
106 | .Fn malloc | | 106 | .Fn malloc |
107 | for the specified size. | | 107 | for the specified size. |
108 | .Pp | | 108 | .Pp |
| | | 109 | When using |
| | | 110 | .Fn realloc |
| | | 111 | one must be careful to avoid the following idiom: |
| | | 112 | .Pp |
| | | 113 | .Bd -literal -offset indent |
| | | 114 | nsize += 50; |
| | | 115 | if ((p = realloc(p, nsize)) == NULL) |
| | | 116 | return (NULL); |
| | | 117 | .Ed |
| | | 118 | .Pp |
| | | 119 | Do not adjust the variable describing how much memory has been allocated |
| | | 120 | until one knows the allocation has been successful. |
| | | 121 | This can cause aberrant program behavior if the incorrect size value is used. |
| | | 122 | In most cases, the above sample will also result in a leak of memory. |
| | | 123 | As stated earlier, a return value of |
| | | 124 | .Dv NULL |
| | | 125 | indicates that the old object still remains allocated. |
| | | 126 | Better code looks like this: |
| | | 127 | .Bd -literal -offset indent |
| | | 128 | newsize = size + 50; |
| | | 129 | if ((p2 = realloc(p, newsize)) == NULL) { |
| | | 130 | if (p) |
| | | 131 | free(p); |
| | | 132 | p = NULL; |
| | | 133 | return (NULL); |
| | | 134 | } |
| | | 135 | p = p2; |
| | | 136 | size = newsize; |
| | | 137 | .Ed |
| | | 138 | .Pp |
109 | The | | 139 | The |
110 | .Fn free | | 140 | .Fn free |
111 | function causes the allocated memory referenced by | | 141 | function causes the allocated memory referenced by |
112 | .Fa ptr | | 142 | .Fa ptr |
113 | to be made available for future allocations. | | 143 | to be made available for future allocations. |
114 | If | | 144 | If |
115 | .Fa ptr | | 145 | .Fa ptr |
116 | is | | 146 | is |
117 | .Dv NULL , | | 147 | .Dv NULL , |
118 | no action occurs. | | 148 | no action occurs. |
119 | .Sh TUNING | | 149 | .Sh TUNING |
120 | Once, when the first call is made to one of these memory allocation | | 150 | Once, when the first call is made to one of these memory allocation |
121 | routines, various flags will be set or reset, which affect the | | 151 | routines, various flags will be set or reset, which affect the |