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 req uired on this page.
33 .TH MALLOC 3 2014-05-21 "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" );
51 bytes and returns a pointer to the allocated memory.
52 .IR "The memory is not initialized" .
59 or a unique pointer value that can later be successfully passed to
64 function frees the memory space pointed to by
66 which must have been returned by a previous call to
73 has already been called before, undefined behavior occurs.
76 is NULL, no operation is performed.
80 function allocates memory for an array of
84 bytes each and returns a pointer to the allocated memory.
85 The memory is set to zero.
94 or a unique pointer value that can later be successfully passed to
99 function changes the size of the memory block pointed to by
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
111 is NULL, then the call is equivalent to
120 is not NULL, then the call is equivalent to
124 is NULL, it must have been returned by an earlier call to
129 If the area pointed to was moved, a
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
145 or by a successful call to
155 function returns no value.
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
162 or NULL if the request fails.
165 was equal to 0, either NULL or a pointer suitable to be passed to
170 fails, the original block is left untouched; it is not freed or moved.
174 By default, Linux follows an optimistic memory allocation strategy.
177 returns non-NULL there is no guarantee that the memory really
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
184 .IR /proc/sys/vm/oom_adj
187 and the Linux kernel source file
188 .IR Documentation/vm/overcommit-accounting .
192 allocates memory from the heap, and adjusts the size of the heap
195 When allocating blocks of memory larger than
199 implementation allocates the memory as a private anonymous mapping using
202 is 128 kB by default, but is adjustable using
204 Allocations performed using
206 are unaffected by the
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
227 and managed with its own mutexes.
229 The UNIX 98 standard requires
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
243 then certain library routines may fail without having
253 are almost always related to heap corruption, such as overflowing
254 an allocated chunk or freeing the same pointer twice.
258 implementation is tunable via environment variables; see
262 .\" http://g.oswego.edu/dl/html/malloc.html
263 .\" A Memory Allocator - by Doug Lea
265 .\" http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html
266 .\" Linux Heap, Contention in free() - David Boreham
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
277 .BR malloc_get_state (3),
280 .BR malloc_usable_size (3),
284 .BR posix_memalign (3)
286 This page is part of release 3.67 of the Linux
289 A description of the project,
290 information about reporting bugs,
291 and the latest version of this page,
293 \%http://www.kernel.org/doc/man\-pages/.