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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu)
7 .\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701
8 .\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
9 .\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
11 .\" FIXME . Review http://austingroupbugs.net/view.php?id=374
12 .\" to see what changes are required on this page.
14 .TH MALLOC 3 2021-03-22 "GNU" "Linux Programmer's Manual"
16 malloc, free, calloc, realloc, reallocarray \- allocate and free dynamic memory
19 .RI ( libc ", " \-lc )
22 .B #include <stdlib.h>
24 .BI "void *malloc(size_t " "size" );
25 .BI "void free(void " "*ptr" );
26 .BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
27 .BI "void *realloc(void " "*ptr" ", size_t " "size" );
28 .BI "void *reallocarray(void " "*ptr" ", size_t " nmemb ", size_t " "size" );
32 Feature Test Macro Requirements for glibc (see
33 .BR feature_test_macros (7)):
40 Glibc 2.28 and earlier:
49 bytes and returns a pointer to the allocated memory.
50 .IR "The memory is not initialized" .
55 returns a unique pointer value that can later be successfully passed to
57 (See "Nonportable behavior" for portability issues.)
61 function frees the memory space pointed to by
63 which must have been returned by a previous call to
68 has already been freed, undefined behavior occurs.
71 is NULL, no operation is performed.
75 function allocates memory for an array of
79 bytes each and returns a pointer to the allocated memory.
80 The memory is set to zero.
87 returns a unique pointer value that can later be successfully passed to
90 If the multiplication of
94 would result in integer overflow, then
98 an integer overflow would not be detected in the following call to
100 with the result that an incorrectly sized block of memory would be allocated:
104 malloc(nmemb * size);
110 function changes the size of the memory block pointed to by
115 The contents of the memory
116 will be unchanged in the range from the start of the region
117 up to the minimum of the old and new sizes.
118 If the new size is larger than the old size, the added memory will
124 is NULL, then the call is equivalent to
134 is not NULL, then the call is equivalent to
136 (but see "Nonportable behavior" for portability issues).
140 is NULL, it must have been returned by an earlier call to
142 or related functions.
143 If the area pointed to was moved, a
149 function changes the size of (and possibly moves)
150 the memory block pointed to by
152 to be large enough for an array of
154 elements, each of which is
157 It is equivalent to the call
161 realloc(ptr, nmemb * size);
169 fails safely in the case where the multiplication would overflow.
170 If such an overflow occurs,
180 functions return a pointer to the allocated memory,
181 which is suitably aligned for any type that fits into
182 the requested size or less.
183 On error, these functions return NULL and set
185 Attempting to allocate more than
187 bytes is considered an error, as an object that large
188 could cause later pointer subtraction to overflow.
192 function returns no value, and preserves
199 functions return NULL if
201 is not NULL and the requested size is zero;
202 this is not considered an error.
203 (See "Nonportable behavior" for portability issues.)
204 Otherwise, the returned pointer may be the same as
206 if the allocation was not moved
207 (e.g., there was room to expand the allocation in-place), or different from
209 if the allocation was moved to a new address.
210 If these functions fail,
211 the original block is left untouched; it is not freed or moved.
218 can fail with the following error:
222 Possibly, the application hit the
230 first appeared in glibc in version 2.26.
233 and related functions rejected sizes greater than
235 starting in glibc 2.30.
240 starting in glibc 2.33.
242 For an explanation of the terms used in this section, see
250 Interface Attribute Value
256 T} Thread safety MT-Safe
266 POSIX.1-2001, POSIX.1-2008, C89, C99.
269 is a nonstandard extension that first appeared in OpenBSD 5.6 and FreeBSD 11.0.
271 By default, Linux follows an optimistic memory allocation strategy.
274 returns non-NULL there is no guarantee that the memory really
276 In case it turns out that the system is out of memory,
277 one or more processes will be killed by the OOM killer.
278 For more information, see the description of
279 .I /proc/sys/vm/overcommit_memory
281 .I /proc/sys/vm/oom_adj
284 and the Linux kernel source file
285 .IR Documentation/vm/overcommit\-accounting.rst .
289 allocates memory from the heap, and adjusts the size of the heap
292 When allocating blocks of memory larger than
296 implementation allocates the memory as a private anonymous mapping using
299 is 128\ kB by default, but is adjustable using
302 allocations performed using
304 were unaffected by the
307 since Linux 4.7, this limit is also enforced for allocations performed using
310 To avoid corruption in multithreaded applications,
311 mutexes are used internally to protect the memory-management
312 data structures employed by these functions.
313 In a multithreaded application in which threads simultaneously
314 allocate and free memory,
315 there could be contention for these mutexes.
316 To scalably handle memory allocation in multithreaded applications,
317 glibc creates additional
318 .I memory allocation arenas
319 if mutex contention is detected.
320 Each arena is a large region of memory that is internally allocated
326 and managed with its own mutexes.
328 If your program uses a private memory allocator,
329 it should do so by replacing
335 The replacement functions must implement the documented glibc behaviors,
338 handling, size-zero allocations, and overflow checking;
339 otherwise, other library routines may crash or operate incorrectly.
340 For example, if the replacement
342 does not preserve errno, then seemingly unrelated library routines may
343 fail without having a valid reason in
345 Private memory allocators may also need to replace other glibc functions;
346 see "Replacing malloc" in the glibc manual for details.
348 Crashes in memory allocators
349 are almost always related to heap corruption, such as overflowing
350 an allocated chunk or freeing the same pointer twice.
354 implementation is tunable via environment variables; see
357 .SS Nonportable behavior
359 these functions when the requested size is zero
361 other implementations may return NULL without setting
363 and portable POSIX programs should tolerate such behavior.
367 POSIX requires memory allocators
371 However, the C standard does not require this, and applications
372 portable to non-POSIX platforms should not assume this.
374 Portable programs should not use private memory allocators,
375 as POSIX and the C standard do not allow replacement of
382 .\" http://g.oswego.edu/dl/html/malloc.html
383 .\" A Memory Allocator - by Doug Lea
385 .\" http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html
386 .\" Linux Heap, Contention in free() - David Boreham
388 .\" http://www.citi.umich.edu/projects/linux-scalability/reports/malloc.html
389 .\" malloc() Performance in a Multithreaded Linux Environment -
390 .\" Check Lever, David Boreham
398 .BR malloc_get_state (3),
401 .BR malloc_usable_size (3),
405 .BR posix_memalign (3)
407 For details of the GNU C library implementation, see
408 .UR https://sourceware.org/glibc/wiki/MallocInternals