OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / original / man3 / malloc.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701
25 .\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
26 .\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
27 .\"
28 .TH MALLOC 3  2009-01-13 "GNU" "Linux Programmer's Manual"
29 .SH NAME
30 calloc, malloc, free, realloc \- Allocate and free dynamic memory
31 .SH SYNOPSIS
32 .nf
33 .B #include <stdlib.h>
34 .sp
35 .BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
36 .br
37 .BI "void *malloc(size_t " "size" );
38 .br
39 .BI "void free(void " "*ptr" );
40 .br
41 .BI "void *realloc(void " "*ptr" ", size_t "  "size" );
42 .fi
43 .SH DESCRIPTION
44 .BR calloc ()
45 allocates memory for an array of
46 .I nmemb
47 elements of
48 .I size
49 bytes each and returns a pointer to the allocated memory.
50 The memory is set to zero.
51 If
52 .I nmemb
53 or
54 .I size
55 is 0, then
56 .BR calloc ()
57 returns either NULL,
58 .\" glibc does this:
59 or a unique pointer value that can later be successfully passed to
60 .BR free ().
61 .PP
62 .BR malloc ()
63 allocates
64 .I size
65 bytes and returns a pointer to the allocated memory.
66 The memory is not cleared.
67 If
68 .I size
69 is 0, then
70 .BR malloc ()
71 returns either NULL,
72 .\" glibc does this:
73 or a unique pointer value that can later be successfully passed to
74 .BR free ().
75 .PP
76 .BR free ()
77 frees the memory space pointed to by
78 .IR ptr ,
79 which must have been returned by a previous call to
80 .BR malloc (),
81 .BR calloc ()
82 or
83 .BR realloc ().
84 Otherwise, or if
85 .I free(ptr)
86 has already been called before, undefined behavior occurs.
87 If
88 .I ptr
89 is NULL, no operation is performed.
90 .PP
91 .BR realloc ()
92 changes the size of the memory block pointed to by
93 .I ptr
94 to
95 .I size
96 bytes.
97 The contents will be unchanged to the minimum of the old and new sizes;
98 newly allocated memory will be uninitialized.
99 If
100 .I ptr
101 is NULL, then the call is equivalent to
102 .IR malloc(size) ,
103 for all values of
104 .IR size ;
105 if
106 .I size
107 is equal to zero,
108 and
109 .I ptr
110 is not NULL, then the call is equivalent to
111 .IR free(ptr) .
112 Unless
113 .I ptr
114 is NULL, it must have been returned by an earlier call to
115 .BR malloc (),
116 .BR calloc ()
117 or
118 .BR realloc ().
119 If the area pointed to was moved, a
120 .I free(ptr)
121 is done.
122 .SH "RETURN VALUE"
123 For
124 .BR calloc ()
125 and
126 .BR malloc (),
127 return a pointer to the allocated memory, which is suitably
128 aligned for any kind of variable.
129 On error, these functions return NULL.
130 NULL may also be returned by a successful call to
131 .BR malloc ()
132 with a
133 .I size
134 of zero,
135 or by a successful call to
136 .BR calloc ()
137 with
138 .I nmemb
139 or
140 .I size
141 equal to zero.
142 .PP
143 .BR free ()
144 returns no value.
145 .PP
146 .BR realloc ()
147 returns a pointer to the newly allocated memory, which is suitably
148 aligned for any kind of variable and may be different from
149 .IR ptr ,
150 or NULL if the request fails.
151 If
152 .I size
153 was equal to 0, either NULL or a pointer suitable to be passed to
154 .BR free ()
155 is returned.
156 If
157 .BR realloc ()
158 fails the original block is left untouched; it is not freed or moved.
159 .SH "CONFORMING TO"
160 C89, C99.
161 .SH NOTES
162 Normally,
163 .BR malloc ()
164 allocates memory from the heap, and adjusts the size of the heap
165 as required, using
166 .BR sbrk (2).
167 When allocating blocks of memory larger than
168 .B MMAP_THRESHOLD
169 bytes, the glibc
170 .BR malloc ()
171 implementation allocates the memory as a private anonymous mapping using
172 .BR mmap (2).
173 .B MMAP_THRESHOLD
174 is 128 kB by default, but is adjustable using
175 .BR mallopt (3).
176 .\" FIXME . there is no mallopt(3) man page yet.
177 Allocations performed using
178 .BR mmap (2)
179 are unaffected by the
180 .B RLIMIT_DATA
181 resource limit (see
182 .BR getrlimit (2)).
183
184 The Unix98 standard requires
185 .BR malloc (),
186 .BR calloc (),
187 and
188 .BR realloc ()
189 to set
190 .I errno
191 to
192 .B ENOMEM
193 upon failure.
194 Glibc assumes that this is done
195 (and the glibc versions of these routines do this); if you
196 use a private malloc implementation that does not set
197 .IR errno ,
198 then certain library routines may fail without having
199 a reason in
200 .IR errno .
201 .LP
202 Crashes in
203 .BR malloc (),
204 .BR calloc (),
205 .BR realloc (),
206 or
207 .BR free ()
208 are almost always related to heap corruption, such as overflowing
209 an allocated chunk or freeing the same pointer twice.
210 .PP
211 Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
212 include a
213 .BR malloc ()
214 implementation which is tunable via environment variables.
215 When
216 .B MALLOC_CHECK_
217 is set, a special (less efficient) implementation is used which
218 is designed to be tolerant against simple errors, such as double
219 calls of
220 .BR free ()
221 with the same argument, or overruns of a single byte (off-by-one
222 bugs).
223 Not all such errors can be protected against, however, and
224 memory leaks can result.
225 If
226 .B MALLOC_CHECK_
227 is set to 0, any detected heap corruption is silently ignored;
228 if set to 1, a diagnostic message is printed on \fIstderr\fP;
229 if set to 2,
230 .BR abort (3)
231 is called immediately;
232 if set to 3, a diagnostic message is printed on \fIstderr\fP
233 and the program is aborted.
234 Using a nonzero
235 .B MALLOC_CHECK_
236 value can be useful because otherwise
237 a crash may happen much later, and the true cause for the problem
238 is then very hard to track down.
239 .SH BUGS
240 By default, Linux follows an optimistic memory allocation strategy.
241 This means that when
242 .BR malloc ()
243 returns non-NULL there is no guarantee that the memory really
244 is available.
245 This is a really bad bug.
246 In case it turns out that the system is out of memory,
247 one or more processes will be killed by the infamous OOM killer.
248 In case Linux is employed under circumstances where it would be
249 less desirable to suddenly lose some randomly picked processes,
250 and moreover the kernel version is sufficiently recent,
251 one can switch off this overcommitting behavior using a command like:
252 .in +4n
253 .nf
254
255 .RB "#" " echo 2 > /proc/sys/vm/overcommit_memory"
256
257 .fi
258 .in
259 See also the kernel Documentation directory, files
260 .I vm/overcommit-accounting
261 and
262 .IR sysctl/vm.txt .
263 .SH "SEE ALSO"
264 .BR brk (2),
265 .\" .BR mallopt (3),
266 .BR mmap (2),
267 .BR alloca (3),
268 .BR posix_memalign (3)