1 .\" (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)
27 .TH MALLOC 3 2007-06-25 "GNU" "Linux Programmer's Manual"
29 calloc, malloc, free, realloc \- Allocate and free dynamic memory
32 .B #include <stdlib.h>
34 .BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
36 .BI "void *malloc(size_t " "size" );
38 .BI "void free(void " "*ptr" );
40 .BI "void *realloc(void " "*ptr" ", size_t " "size" );
44 allocates memory for an array of
48 bytes each and returns a pointer to the allocated memory.
49 The memory is set to zero.
54 bytes and returns a pointer to the allocated memory.
55 The memory is not cleared.
58 frees the memory space pointed to by
60 which must have been returned by a previous call to
67 has already been called before, undefined behavior occurs.
70 is NULL, no operation is performed.
73 changes the size of the memory block pointed to by
78 The contents will be unchanged to the minimum of the old and new sizes;
79 newly allocated memory will be uninitialized.
82 is NULL, the call is equivalent to
87 the call is equivalent to
91 is NULL, it must have been returned by an earlier call to
96 If the area pointed to was moved, a
104 the value returned is a pointer to the allocated memory, which is suitably
105 aligned for any kind of variable, or NULL if the request fails.
111 returns a pointer to the newly allocated memory, which is suitably
112 aligned for any kind of variable and may be different from
114 or NULL if the request fails.
117 was equal to 0, either NULL or a pointer suitable to be passed to
122 fails the original block is left untouched; it is not freed or moved.
126 The Unix98 standard requires
133 to ENOMEM upon failure.
134 Glibc assumes that this is done
135 (and the glibc versions of these routines do this); if you
136 use a private malloc implementation that does not set
138 then certain library routines may fail without having
148 are almost always related to heap corruption, such as overflowing
149 an allocated chunk or freeing the same pointer twice.
151 Recent versions of Linux libc (later than 5.4.23) and GNU libc (2.x)
152 include a malloc implementation which is tunable via environment
156 is set, a special (less efficient) implementation is used which
157 is designed to be tolerant against simple errors, such as double
160 with the same argument, or overruns of a single byte (off-by-one
162 Not all such errors can be protected against, however, and
163 memory leaks can result.
166 is set to 0, any detected heap corruption is silently ignored;
167 if set to 1, a diagnostic message is printed on \fIstderr\fP;
170 is called immediately;
171 if set to 3, a diagnostic message is printed on \fIstderr\fP
172 and the program is aborted.
175 value can be useful because otherwise
176 a crash may happen much later, and the true cause for the problem
177 is then very hard to track down.
179 By default, Linux follows an optimistic memory allocation strategy.
182 returns non-NULL there is no guarantee that the memory really
184 This is a really bad bug.
185 In case it turns out that the system is out of memory,
186 one or more processes will be killed by the infamous OOM killer.
187 In case Linux is employed under circumstances where it would be
188 less desirable to suddenly lose some randomly picked processes,
189 and moreover the kernel version is sufficiently recent,
190 one can switch off this overcommitting behavior using a command like:
194 # echo 2 > /proc/sys/vm/overcommit_memory
198 See also the kernel Documentation directory, files
199 .I vm/overcommit-accounting
204 .BR posix_memalign (3)