1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701
25 .\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
26 .\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
28 .TH MALLOC 3 2010-10-18 "GNU" "Linux Programmer's Manual"
30 malloc, free, calloc, realloc \- Allocate and free dynamic memory
33 .B #include <stdlib.h>
35 .BI "void *malloc(size_t " "size" );
36 .BI "void free(void " "*ptr" );
37 .BI "void *realloc(void " "*ptr" ", size_t " "size" );
38 .BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
46 bytes and returns a pointer to the allocated memory.
47 .IR "The memory is not initialized" .
54 or a unique pointer value that can later be successfully passed to
59 function frees the memory space pointed to by
61 which must have been returned by a previous call to
68 has already been called before, 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.
89 or a unique pointer value that can later be successfully passed to
94 function changes the size of the memory block pointed to by
99 The contents will be unchanged in the range from the start of the region
100 up to the minimum of the old and new sizes.
101 If the new size is larger than the old size, the added memory will
106 is NULL, then the call is equivalent to
115 is not NULL, then the call is equivalent to
119 is NULL, it must have been returned by an earlier call to
124 If the area pointed to was moved, a
132 functions return a pointer to the allocated memory
133 that is suitably aligned for any kind of variable.
134 On error, these functions return NULL.
135 NULL may also be returned by a successful call to
140 or by a successful call to
150 function returns no value.
154 function returns a pointer to the newly allocated memory, which is suitably
155 aligned for any kind of variable and may be different from
157 or NULL if the request fails.
160 was equal to 0, either NULL or a pointer suitable to be passed to
165 fails the original block is left untouched; it is not freed or moved.
169 By default, Linux follows an optimistic memory allocation strategy.
172 returns non-NULL there is no guarantee that the memory really
174 In case it turns out that the system is out of memory,
175 one or more processes will be killed by the OOM killer.
176 For more information, see the description of
177 .IR /proc/sys/vm/overcommit_memory
179 .IR /proc/sys/vm/oom_adj
182 and the kernel source file
183 .IR Documentation/vm/overcommit-accounting .
187 allocates memory from the heap, and adjusts the size of the heap
190 When allocating blocks of memory larger than
194 implementation allocates the memory as a private anonymous mapping using
197 is 128 kB by default, but is adjustable using
199 .\" FIXME . there is no mallopt(3) man page yet.
200 Allocations performed using
202 are unaffected by the
207 The UNIX 98 standard requires
217 Glibc assumes that this is done
218 (and the glibc versions of these routines do this); if you
219 use a private malloc implementation that does not set
221 then certain library routines may fail without having
231 are almost always related to heap corruption, such as overflowing
232 an allocated chunk or freeing the same pointer twice.
234 Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
237 implementation which is tunable via environment variables.
240 is set, a special (less efficient) implementation is used which
241 is designed to be tolerant against simple errors, such as double
244 with the same argument, or overruns of a single byte (off-by-one
246 Not all such errors can be protected against, however, and
247 memory leaks can result.
250 is set to 0, any detected heap corruption is silently ignored;
251 if set to 1, a diagnostic message is printed on \fIstderr\fP;
254 is called immediately;
255 if set to 3, a diagnostic message is printed on \fIstderr\fP
256 and the program is aborted.
259 value can be useful because otherwise
260 a crash may happen much later, and the true cause for the problem
261 is then very hard to track down.
267 .BR posix_memalign (3)