1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
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().
30 .\" FIXME . Review http://austingroupbugs.net/view.php?id=374
31 .\" to see what changes are required on this page.
33 .TH MALLOC 3 2017-09-15 "GNU" "Linux Programmer's Manual"
35 malloc, free, calloc, realloc \- allocate and free dynamic memory
38 .B #include <stdlib.h>
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" );
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
58 Glibc 2.28 and earlier:
67 bytes and returns a pointer to the allocated memory.
68 .IR "The memory is not initialized" .
75 or a unique pointer value that can later be successfully passed to
80 function frees the memory space pointed to by
82 which must have been returned by a previous call to
89 has already been called before, undefined behavior occurs.
92 is NULL, no operation is performed.
96 function allocates memory for an array of
100 bytes each and returns a pointer to the allocated memory.
101 The memory is set to zero.
110 or a unique pointer value that can later be successfully passed to
112 If the multiplication of
116 would result in integer overflow, then
120 an integer overflow would not be detected in the following call to
122 with the result that an incorrectly sized block of memory would be allocated:
126 malloc(nmemb * size);
132 function changes the size of the memory block pointed to by
137 The contents will be unchanged in the range from the start of the region
138 up to the minimum of the old and new sizes.
139 If the new size is larger than the old size, the added memory will
144 is NULL, then the call is equivalent to
153 is not NULL, then the call is equivalent to
157 is NULL, it must have been returned by an earlier call to
162 If the area pointed to was moved, a
168 function changes the size of the memory block pointed to by
170 to be large enough for an array of
172 elements, each of which is
175 It is equivalent to the call
178 realloc(ptr, nmemb * size);
185 fails safely in the case where the multiplication would overflow.
186 If such an overflow occurs,
192 and leaves the original block of memory unchanged.
198 functions return a pointer to the allocated memory,
199 which is suitably aligned for any built-in type.
200 On error, these functions return NULL.
201 NULL may also be returned by a successful call to
206 or by a successful call to
216 function returns no value.
220 function returns a pointer to the newly allocated memory, which is suitably
221 aligned for any built-in type and may be different from
223 or NULL if the request fails.
226 was equal to 0, either NULL or a pointer suitable to be passed to
231 fails, the original block is left untouched; it is not freed or moved.
235 function returns a pointer to the newly allocated memory.
237 it returns NULL and the original block of memory is left untouched.
244 can fail with the following error:
248 Possibly, the application hit the
255 For an explanation of the terms used in this section, see
261 Interface Attribute Value
268 T} Thread safety MT-Safe
275 POSIX.1-2001, POSIX.1-2008, C89, C99.
278 is a nonstandard extension that first appeared in OpenBSD 5.6 and FreeBSD 11.0.
280 By default, Linux follows an optimistic memory allocation strategy.
283 returns non-NULL there is no guarantee that the memory really
285 In case it turns out that the system is out of memory,
286 one or more processes will be killed by the OOM killer.
287 For more information, see the description of
288 .IR /proc/sys/vm/overcommit_memory
290 .IR /proc/sys/vm/oom_adj
293 and the Linux kernel source file
294 .IR Documentation/vm/overcommit-accounting.rst .
298 allocates memory from the heap, and adjusts the size of the heap
301 When allocating blocks of memory larger than
305 implementation allocates the memory as a private anonymous mapping using
308 is 128\ kB by default, but is adjustable using
311 allocations performed using
313 were unaffected by the
316 since Linux 4.7, this limit is also enforced for allocations performed using
319 To avoid corruption in multithreaded applications,
320 mutexes are used internally to protect the memory-management
321 data structures employed by these functions.
322 In a multithreaded application in which threads simultaneously
323 allocate and free memory,
324 there could be contention for these mutexes.
325 To scalably handle memory allocation in multithreaded applications,
326 glibc creates additional
327 .IR "memory allocation arenas"
328 if mutex contention is detected.
329 Each arena is a large region of memory that is internally allocated
335 and managed with its own mutexes.
347 Glibc assumes that this is done
348 (and the glibc versions of these routines do this); if you
349 use a private malloc implementation that does not set
351 then certain library routines may fail without having
361 are almost always related to heap corruption, such as overflowing
362 an allocated chunk or freeing the same pointer twice.
366 implementation is tunable via environment variables; see
370 .\" http://g.oswego.edu/dl/html/malloc.html
371 .\" A Memory Allocator - by Doug Lea
373 .\" http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html
374 .\" Linux Heap, Contention in free() - David Boreham
376 .\" http://www.citi.umich.edu/projects/linux-scalability/reports/malloc.html
377 .\" malloc() Performance in a Multithreaded Linux Environment -
378 .\" Check Lever, David Boreham
386 .BR malloc_get_state (3),
389 .BR malloc_usable_size (3),
393 .BR posix_memalign (3)
395 For details of the GNU C library implementation, see
396 .UR https://sourceware.org/glibc/wiki/MallocInternals