]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/malloc.3
getrlimit.2, mmap.2, malloc.3: RLIMIT_DATA affects mmap (2) since Linux 4.7
[thirdparty/man-pages.git] / 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 required on this page.
32 .\"
33 .TH MALLOC 3 2017-07-13 "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 .PP
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 .BI "void *reallocarray(void " "*ptr" ", size_t " nmemb ", size_t " "size" );
45 .fi
46 .PP
47 .in -4n
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
50 .in
51 .PP
52 .BR reallocarray ():
53 .br
54 .RS 4
55 .ad l
56 _GNU_SOURCE
57 .RE
58 .ad
59 .SH DESCRIPTION
60 .PP
61 The
62 .BR malloc ()
63 function allocates
64 .I size
65 bytes and returns a pointer to the allocated memory.
66 .IR "The memory is not initialized" .
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 The
77 .BR free ()
78 function frees the memory space pointed to by
79 .IR ptr ,
80 which must have been returned by a previous call to
81 .BR malloc (),
82 .BR calloc (),
83 or
84 .BR realloc ().
85 Otherwise, or if
86 .I free(ptr)
87 has already been called before, undefined behavior occurs.
88 If
89 .I ptr
90 is NULL, no operation is performed.
91 .PP
92 The
93 .BR calloc ()
94 function allocates memory for an array of
95 .I nmemb
96 elements of
97 .I size
98 bytes each and returns a pointer to the allocated memory.
99 The memory is set to zero.
100 If
101 .I nmemb
102 or
103 .I size
104 is 0, then
105 .BR calloc ()
106 returns either NULL,
107 .\" glibc does this:
108 or a unique pointer value that can later be successfully passed to
109 .BR free ().
110 .PP
111 The
112 .BR realloc ()
113 function changes the size of the memory block pointed to by
114 .I ptr
115 to
116 .I size
117 bytes.
118 The contents will be unchanged in the range from the start of the region
119 up to the minimum of the old and new sizes.
120 If the new size is larger than the old size, the added memory will
121 .I not
122 be initialized.
123 If
124 .I ptr
125 is NULL, then the call is equivalent to
126 .IR malloc(size) ,
127 for all values of
128 .IR size ;
129 if
130 .I size
131 is equal to zero,
132 and
133 .I ptr
134 is not NULL, then the call is equivalent to
135 .IR free(ptr) .
136 Unless
137 .I ptr
138 is NULL, it must have been returned by an earlier call to
139 .BR malloc (),
140 .BR calloc (),
141 or
142 .BR realloc ().
143 If the area pointed to was moved, a
144 .I free(ptr)
145 is done.
146 .PP
147 The
148 .BR reallocarray ()
149 function changes the size of the memory block pointed to by
150 .I ptr
151 to be large enough for an array of
152 .I nmemb
153 elements, each of which is
154 .I size
155 bytes.
156 It is equivalent to the call
157 .PP
158 .in +4n
159 realloc(ptr, nmemb * size);
160 .in
161 .PP
162 However, unlike that
163 .BR realloc ()
164 call,
165 .BR reallocarray ()
166 fails safely in the case where the multiplication would overflow.
167 If such an overflow occurs,
168 .BR reallocarray ()
169 returns NULL, sets
170 .I errno
171 to
172 .BR ENOMEM ,
173 and leaves the original block of memory unchanged.
174 .SH RETURN VALUE
175 The
176 .BR malloc ()
177 and
178 .BR calloc ()
179 functions return a pointer to the allocated memory,
180 which is suitably aligned for any built-in type.
181 On error, these functions return NULL.
182 NULL may also be returned by a successful call to
183 .BR malloc ()
184 with a
185 .I size
186 of zero,
187 or by a successful call to
188 .BR calloc ()
189 with
190 .I nmemb
191 or
192 .I size
193 equal to zero.
194 .PP
195 The
196 .BR free ()
197 function returns no value.
198 .PP
199 The
200 .BR realloc ()
201 function returns a pointer to the newly allocated memory, which is suitably
202 aligned for any built-in type and may be different from
203 .IR ptr ,
204 or NULL if the request fails.
205 If
206 .I size
207 was equal to 0, either NULL or a pointer suitable to be passed to
208 .BR free ()
209 is returned.
210 If
211 .BR realloc ()
212 fails, the original block is left untouched; it is not freed or moved.
213 .PP
214 On success, the
215 .BR reallocarray ()
216 function returns a pointer to the newly allocated memory.
217 On failure,
218 it returns NULL and the original block of memory is left untouched.
219 .SH ERRORS
220 .BR calloc (),
221 .BR malloc (),
222 .BR realloc (),
223 and
224 .BR reallocarray ()
225 can fail with the following error:
226 .TP
227 .B ENOMEM
228 Out of memory.
229 Possibly, the application hit the
230 .BR RLIMIT_AS
231 or
232 .BR RLIMIT_DATA
233 limit described in
234 .BR getrlimit (2).
235 .SH ATTRIBUTES
236 For an explanation of the terms used in this section, see
237 .BR attributes (7).
238 .TS
239 allbox;
240 lbw20 lb lb
241 l l l.
242 Interface Attribute Value
243 T{
244 .BR malloc (),
245 .BR free (),
246 .br
247 .BR calloc (),
248 .BR realloc ()
249 T} Thread safety MT-Safe
250 .TE
251 .SH CONFORMING TO
252 .BR malloc (),
253 .BR free (),
254 .BR calloc (),
255 .BR realloc ():
256 POSIX.1-2001, POSIX.1-2008, C89, C99.
257 .PP
258 .BR reallocarray ()
259 is a nonstandard extension that first appeared in OpenBSD 5.6 and FreeBSD 11.0.
260 .SH NOTES
261 By default, Linux follows an optimistic memory allocation strategy.
262 This means that when
263 .BR malloc ()
264 returns non-NULL there is no guarantee that the memory really
265 is available.
266 In case it turns out that the system is out of memory,
267 one or more processes will be killed by the OOM killer.
268 For more information, see the description of
269 .IR /proc/sys/vm/overcommit_memory
270 and
271 .IR /proc/sys/vm/oom_adj
272 in
273 .BR proc (5),
274 and the Linux kernel source file
275 .IR Documentation/vm/overcommit-accounting .
276 .PP
277 Normally,
278 .BR malloc ()
279 allocates memory from the heap, and adjusts the size of the heap
280 as required, using
281 .BR sbrk (2).
282 When allocating blocks of memory larger than
283 .B MMAP_THRESHOLD
284 bytes, the glibc
285 .BR malloc ()
286 implementation allocates the memory as a private anonymous mapping using
287 .BR mmap (2).
288 .B MMAP_THRESHOLD
289 is 128\ kB by default, but is adjustable using
290 .BR mallopt (3).
291 Allocations performed using
292 .BR mmap (2)
293 are unaffected by the
294 .B RLIMIT_DATA
295 resource limit only prior to Linux kernel 4.7 (see
296 .BR getrlimit (2)).
297 .PP
298 To avoid corruption in multithreaded applications,
299 mutexes are used internally to protect the memory-management
300 data structures employed by these functions.
301 In a multithreaded application in which threads simultaneously
302 allocate and free memory,
303 there could be contention for these mutexes.
304 To scalably handle memory allocation in multithreaded applications,
305 glibc creates additional
306 .IR "memory allocation arenas"
307 if mutex contention is detected.
308 Each arena is a large region of memory that is internally allocated
309 by the system
310 (using
311 .BR brk (2)
312 or
313 .BR mmap (2)),
314 and managed with its own mutexes.
315 .PP
316 SUSv2 requires
317 .BR malloc (),
318 .BR calloc (),
319 and
320 .BR realloc ()
321 to set
322 .I errno
323 to
324 .B ENOMEM
325 upon failure.
326 Glibc assumes that this is done
327 (and the glibc versions of these routines do this); if you
328 use a private malloc implementation that does not set
329 .IR errno ,
330 then certain library routines may fail without having
331 a reason in
332 .IR errno .
333 .PP
334 Crashes in
335 .BR malloc (),
336 .BR calloc (),
337 .BR realloc (),
338 or
339 .BR free ()
340 are almost always related to heap corruption, such as overflowing
341 an allocated chunk or freeing the same pointer twice.
342 .PP
343 The
344 .BR malloc ()
345 implementation is tunable via environment variables; see
346 .BR mallopt (3)
347 for details.
348 .SH SEE ALSO
349 .\" http://g.oswego.edu/dl/html/malloc.html
350 .\" A Memory Allocator - by Doug Lea
351 .\"
352 .\" http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html
353 .\" Linux Heap, Contention in free() - David Boreham
354 .\"
355 .\" http://www.citi.umich.edu/projects/linux-scalability/reports/malloc.html
356 .\" malloc() Performance in a Multithreaded Linux Environment -
357 .\" Check Lever, David Boreham
358 .\"
359 .ad l
360 .nh
361 .BR brk (2),
362 .BR mmap (2),
363 .BR alloca (3),
364 .BR malloc_get_state (3),
365 .BR malloc_info (3),
366 .BR malloc_trim (3),
367 .BR malloc_usable_size (3),
368 .BR mallopt (3),
369 .BR mcheck (3),
370 .BR mtrace (3),
371 .BR posix_memalign (3)