1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\" and Copyright i2007, 2012, 2018, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu)
27 .\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701
28 .\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
29 .\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
31 .\" FIXME . Review http://austingroupbugs.net/view.php?id=374
32 .\" to see what changes are required on this page.
34 .TH MALLOC 3 2021-03-22 "GNU" "Linux Programmer's Manual"
36 malloc, free, calloc, realloc, reallocarray \- allocate and free dynamic memory
39 .B #include <stdlib.h>
41 .BI "void *malloc(size_t " "size" );
42 .BI "void free(void " "*ptr" );
43 .BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
44 .BI "void *realloc(void " "*ptr" ", size_t " "size" );
45 .BI "void *reallocarray(void " "*ptr" ", size_t " nmemb ", size_t " "size" );
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
57 Glibc 2.28 and earlier:
66 bytes and returns a pointer to the allocated memory.
67 .IR "The memory is not initialized" .
72 returns a unique pointer value that can later be successfully passed to
74 (See "Nonportable behavior" for portability issues.)
78 function frees the memory space pointed to by
80 which must have been returned by a previous call to
85 has already been freed, undefined behavior occurs.
88 is NULL, no operation is performed.
92 function allocates memory for an array of
96 bytes each and returns a pointer to the allocated memory.
97 The memory is set to zero.
104 returns a unique pointer value that can later be successfully passed to
107 If the multiplication of
111 would result in integer overflow, then
115 an integer overflow would not be detected in the following call to
117 with the result that an incorrectly sized block of memory would be allocated:
121 malloc(nmemb * size);
127 function changes the size of the memory block pointed to by
132 The contents will be unchanged in the range from the start of the region
133 up to the minimum of the old and new sizes.
134 If the new size is larger than the old size, the added memory will
140 is NULL, then the call is equivalent to
150 is not NULL, then the call is equivalent to
152 (but see "Nonportable behavior" for portability issues).
156 is NULL, it must have been returned by an earlier call to
158 or related functions.
159 If the area pointed to was moved, a
165 function changes the size of the memory block pointed to by
167 to be large enough for an array of
169 elements, each of which is
172 It is equivalent to the call
176 realloc(ptr, nmemb * size);
184 fails safely in the case where the multiplication would overflow.
185 If such an overflow occurs,
195 functions return a pointer to the allocated memory,
196 which is suitably aligned for any type that fits into
197 the requested size or less.
198 On error, these functions return NULL and set
200 Attempting to allocate more than
202 bytes is considered an error, as an object that large
203 could cause later pointer subtraction to overflow.
207 function returns no value, and preserves
214 functions return NULL if
216 is not NULL and the requested size is zero;
217 this is not considered an error.
218 (See "Nonportable behavior" for portability issues.)
219 Otherwise, the returned pointer may be the same as
221 if the allocation was not moved
222 (e.g., there was room to expand the allocation in-place), or different from
224 if the allocation was moved to a new address.
225 If these functions fail,
226 the original block is left untouched; it is not freed or moved.
233 can fail with the following error:
237 Possibly, the application hit the
245 first appeared in glibc in version 2.26.
248 and related functions rejected sizes greater than
250 starting in glibc 2.30.
255 starting in glibc 2.33.
257 For an explanation of the terms used in this section, see
265 Interface Attribute Value
271 T} Thread safety MT-Safe
281 POSIX.1-2001, POSIX.1-2008, C89, C99.
284 is a nonstandard extension that first appeared in OpenBSD 5.6 and FreeBSD 11.0.
286 By default, Linux follows an optimistic memory allocation strategy.
289 returns non-NULL there is no guarantee that the memory really
291 In case it turns out that the system is out of memory,
292 one or more processes will be killed by the OOM killer.
293 For more information, see the description of
294 .IR /proc/sys/vm/overcommit_memory
296 .IR /proc/sys/vm/oom_adj
299 and the Linux kernel source file
300 .IR Documentation/vm/overcommit\-accounting.rst .
304 allocates memory from the heap, and adjusts the size of the heap
307 When allocating blocks of memory larger than
311 implementation allocates the memory as a private anonymous mapping using
314 is 128\ kB by default, but is adjustable using
317 allocations performed using
319 were unaffected by the
322 since Linux 4.7, this limit is also enforced for allocations performed using
325 To avoid corruption in multithreaded applications,
326 mutexes are used internally to protect the memory-management
327 data structures employed by these functions.
328 In a multithreaded application in which threads simultaneously
329 allocate and free memory,
330 there could be contention for these mutexes.
331 To scalably handle memory allocation in multithreaded applications,
332 glibc creates additional
333 .IR "memory allocation arenas"
334 if mutex contention is detected.
335 Each arena is a large region of memory that is internally allocated
341 and managed with its own mutexes.
343 If your program uses a private memory allocator,
344 it should do so by replacing
350 The replacement functions must implement the documented glibc behaviors,
353 handling, size-zero allocations, and overflow checking;
354 otherwise, other library routines may crash or operate incorrectly.
355 For example, if the replacement
357 does not preserve errno, then seemingly unrelated library routines may
358 fail without having a valid reason in
360 Private memory allocators may also need to replace other glibc functions;
361 see "Replacing malloc" in the glibc manual for details.
363 Crashes in memory allocators
364 are almost always related to heap corruption, such as overflowing
365 an allocated chunk or freeing the same pointer twice.
369 implementation is tunable via environment variables; see
372 .SS Nonportable behavior
374 these functions when the requested size is zero
376 other implementations may return NULL without setting
378 and portable POSIX programs should tolerate such behavior.
382 POSIX requires memory allocators
386 However, the C standard does not require this, and applications
387 portable to non-POSIX platforms should not assume this.
389 Portable programs should not use private memory allocators,
390 as POSIX and the C standard do not allow replacement of
397 .\" http://g.oswego.edu/dl/html/malloc.html
398 .\" A Memory Allocator - by Doug Lea
400 .\" http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html
401 .\" Linux Heap, Contention in free() - David Boreham
403 .\" http://www.citi.umich.edu/projects/linux-scalability/reports/malloc.html
404 .\" malloc() Performance in a Multithreaded Linux Environment -
405 .\" Check Lever, David Boreham
413 .BR malloc_get_state (3),
416 .BR malloc_usable_size (3),
420 .BR posix_memalign (3)
422 For details of the GNU C library implementation, see
423 .UR https://sourceware.org/glibc/wiki/MallocInternals