OSDN Git Service

2ccf20abde1bf8cc85cbc0d80685dd072d977378
[linuxjm/LDP_man-pages.git] / original / man3 / malloc.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu)
26 .\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701
27 .\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
28 .\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
29 .\"
30 .\" FIXME: Review http://austingroupbugs.net/view.php?id=374
31 .\"     to see what changes are req     uired on this page.
32 .\"
33 .TH MALLOC 3  2014-05-21 "GNU" "Linux Programmer's Manual"
34 .SH NAME
35 malloc, free, calloc, realloc \- allocate and free dynamic memory
36 .SH SYNOPSIS
37 .nf
38 .B #include <stdlib.h>
39 .sp
40 .BI "void *malloc(size_t " "size" );
41 .BI "void free(void " "*ptr" );
42 .BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
43 .BI "void *realloc(void " "*ptr" ", size_t "  "size" );
44 .fi
45 .SH DESCRIPTION
46 .PP
47 The
48 .BR malloc ()
49 function allocates
50 .I size
51 bytes and returns a pointer to the allocated memory.
52 .IR "The memory is not initialized" .
53 If
54 .I size
55 is 0, then
56 .BR malloc ()
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 The
63 .BR free ()
64 function frees the memory space pointed to by
65 .IR ptr ,
66 which must have been returned by a previous call to
67 .BR malloc (),
68 .BR calloc ()
69 or
70 .BR realloc ().
71 Otherwise, or if
72 .I free(ptr)
73 has already been called before, undefined behavior occurs.
74 If
75 .I ptr
76 is NULL, no operation is performed.
77 .PP
78 The
79 .BR calloc ()
80 function allocates memory for an array of
81 .I nmemb
82 elements of
83 .I size
84 bytes each and returns a pointer to the allocated memory.
85 The memory is set to zero.
86 If
87 .I nmemb
88 or
89 .I size
90 is 0, then
91 .BR calloc ()
92 returns either NULL,
93 .\" glibc does this:
94 or a unique pointer value that can later be successfully passed to
95 .BR free ().
96 .PP
97 The
98 .BR realloc ()
99 function changes the size of the memory block pointed to by
100 .I ptr
101 to
102 .I size
103 bytes.
104 The contents will be unchanged in the range from the start of the region
105 up to the minimum of the old and new sizes.
106 If the new size is larger than the old size, the added memory will
107 .I not
108 be initialized.
109 If
110 .I ptr
111 is NULL, then the call is equivalent to
112 .IR malloc(size) ,
113 for all values of
114 .IR size ;
115 if
116 .I size
117 is equal to zero,
118 and
119 .I ptr
120 is not NULL, then the call is equivalent to
121 .IR free(ptr) .
122 Unless
123 .I ptr
124 is NULL, it must have been returned by an earlier call to
125 .BR malloc (),
126 .BR calloc ()
127 or
128 .BR realloc ().
129 If the area pointed to was moved, a
130 .I free(ptr)
131 is done.
132 .SH RETURN VALUE
133 The
134 .BR malloc ()
135 and
136 .BR calloc ()
137 functions return a pointer to the allocated memory,
138 which is suitably aligned for any built-in type.
139 On error, these functions return NULL.
140 NULL may also be returned by a successful call to
141 .BR malloc ()
142 with a
143 .I size
144 of zero,
145 or by a successful call to
146 .BR calloc ()
147 with
148 .I nmemb
149 or
150 .I size
151 equal to zero.
152 .PP
153 The
154 .BR free ()
155 function returns no value.
156 .PP
157 The
158 .BR realloc ()
159 function returns a pointer to the newly allocated memory, which is suitably
160 aligned for any built-in type and may be different from
161 .IR ptr ,
162 or NULL if the request fails.
163 If
164 .I size
165 was equal to 0, either NULL or a pointer suitable to be passed to
166 .BR free ()
167 is returned.
168 If
169 .BR realloc ()
170 fails, the original block is left untouched; it is not freed or moved.
171 .SH CONFORMING TO
172 C89, C99.
173 .SH NOTES
174 By default, Linux follows an optimistic memory allocation strategy.
175 This means that when
176 .BR malloc ()
177 returns non-NULL there is no guarantee that the memory really
178 is available.
179 In case it turns out that the system is out of memory,
180 one or more processes will be killed by the OOM killer.
181 For more information, see the description of
182 .IR /proc/sys/vm/overcommit_memory
183 and
184 .IR /proc/sys/vm/oom_adj
185 in
186 .BR proc (5),
187 and the Linux kernel source file
188 .IR Documentation/vm/overcommit-accounting .
189
190 Normally,
191 .BR malloc ()
192 allocates memory from the heap, and adjusts the size of the heap
193 as required, using
194 .BR sbrk (2).
195 When allocating blocks of memory larger than
196 .B MMAP_THRESHOLD
197 bytes, the glibc
198 .BR malloc ()
199 implementation allocates the memory as a private anonymous mapping using
200 .BR mmap (2).
201 .B MMAP_THRESHOLD
202 is 128 kB by default, but is adjustable using
203 .BR mallopt (3).
204 Allocations performed using
205 .BR mmap (2)
206 are unaffected by the
207 .B RLIMIT_DATA
208 resource limit (see
209 .BR getrlimit (2)).
210
211 To avoid corruption in multithreaded applications,
212 mutexes are used internally to protect the memory-management
213 data structures employed by these functions.
214 In a multithreaded application in which threads simultaneously
215 allocate and free memory,
216 there could be contention for these mutexes.
217 To scalably handle memory allocation in multithreaded applications,
218 glibc creates additional
219 .IR "memory allocation arenas"
220 if mutex contention is detected.
221 Each arena is a large region of memory that is internally allocated
222 by the system
223 (using
224 .BR brk (2)
225 or
226 .BR mmap (2)),
227 and managed with its own mutexes.
228
229 The UNIX 98 standard requires
230 .BR malloc (),
231 .BR calloc (),
232 and
233 .BR realloc ()
234 to set
235 .I errno
236 to
237 .B ENOMEM
238 upon failure.
239 Glibc assumes that this is done
240 (and the glibc versions of these routines do this); if you
241 use a private malloc implementation that does not set
242 .IR errno ,
243 then certain library routines may fail without having
244 a reason in
245 .IR errno .
246 .LP
247 Crashes in
248 .BR malloc (),
249 .BR calloc (),
250 .BR realloc (),
251 or
252 .BR free ()
253 are almost always related to heap corruption, such as overflowing
254 an allocated chunk or freeing the same pointer twice.
255 .PP
256 The
257 .BR malloc ()
258 implementation is tunable via environment variables; see
259 .BR mallopt (3)
260 for details.
261 .SH SEE ALSO
262 .\" http://g.oswego.edu/dl/html/malloc.html
263 .\" A Memory Allocator - by Doug Lea
264 .\"
265 .\" http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html
266 .\" Linux Heap, Contention in free() - David Boreham
267 .\"
268 .\" http://www.citi.umich.edu/projects/linux-scalability/reports/malloc.html
269 .\" malloc() Performance in a Multithreaded Linux Environment -
270 .\"     Check Lever, David Boreham
271 .\"
272 .ad l
273 .nh
274 .BR brk (2),
275 .BR mmap (2),
276 .BR alloca (3),
277 .BR malloc_get_state (3),
278 .BR malloc_info (3),
279 .BR malloc_trim (3),
280 .BR malloc_usable_size (3),
281 .BR mallopt (3),
282 .BR mcheck (3),
283 .BR mtrace (3),
284 .BR posix_memalign (3)
285 .SH COLOPHON
286 This page is part of release 3.67 of the Linux
287 .I man-pages
288 project.
289 A description of the project,
290 information about reporting bugs,
291 and the latest version of this page,
292 can be found at
293 \%http://www.kernel.org/doc/man\-pages/.