]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/malloc.3
2ae381a548baa4fa38c625cc361ab9249de25b17
[thirdparty/man-pages.git] / man3 / malloc.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
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().
27 .\"
28 .TH MALLOC 3 2010-10-03 "GNU" "Linux Programmer's Manual"
29 .SH NAME
30 malloc, free, calloc, realloc \- Allocate and free dynamic memory
31 .SH SYNOPSIS
32 .nf
33 .B #include <stdlib.h>
34 .sp
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" );
39 .fi
40 .SH DESCRIPTION
41 .PP
42 The
43 .BR malloc ()
44 function allocates
45 .I size
46 bytes and returns a pointer to the allocated memory.
47 The memory is not cleared.
48 If
49 .I size
50 is 0, then
51 .BR malloc ()
52 returns either NULL,
53 .\" glibc does this:
54 or a unique pointer value that can later be successfully passed to
55 .BR free ().
56 .PP
57 The
58 .BR free ()
59 function frees the memory space pointed to by
60 .IR ptr ,
61 which must have been returned by a previous call to
62 .BR malloc (),
63 .BR calloc ()
64 or
65 .BR realloc ().
66 Otherwise, or if
67 .I free(ptr)
68 has already been called before, undefined behavior occurs.
69 If
70 .I ptr
71 is NULL, no operation is performed.
72 .PP
73 The
74 .BR calloc ()
75 function allocates memory for an array of
76 .I nmemb
77 elements of
78 .I size
79 bytes each and returns a pointer to the allocated memory.
80 The memory is set to zero.
81 If
82 .I nmemb
83 or
84 .I size
85 is 0, then
86 .BR calloc ()
87 returns either NULL,
88 .\" glibc does this:
89 or a unique pointer value that can later be successfully passed to
90 .BR free ().
91 .PP
92 The
93 .BR realloc ()
94 function changes the size of the memory block pointed to by
95 .I ptr
96 to
97 .I size
98 bytes.
99 The contents will be unchanged n the range from the start of the region
100 up to the minimum of the old and new sizes;
101 newly allocated memory will be uninitialized.
102 If
103 .I ptr
104 is NULL, then the call is equivalent to
105 .IR malloc(size) ,
106 for all values of
107 .IR size ;
108 if
109 .I size
110 is equal to zero,
111 and
112 .I ptr
113 is not NULL, then the call is equivalent to
114 .IR free(ptr) .
115 Unless
116 .I ptr
117 is NULL, it must have been returned by an earlier call to
118 .BR malloc (),
119 .BR calloc ()
120 or
121 .BR realloc ().
122 If the area pointed to was moved, a
123 .I free(ptr)
124 is done.
125 .SH "RETURN VALUE"
126 The
127 .BR malloc ()
128 and
129 .BR calloc ()
130 functions return a pointer to the allocated memory
131 that is suitably aligned for any kind of variable.
132 On error, these functions return NULL.
133 NULL may also be returned by a successful call to
134 .BR malloc ()
135 with a
136 .I size
137 of zero,
138 or by a successful call to
139 .BR calloc ()
140 with
141 .I nmemb
142 or
143 .I size
144 equal to zero.
145 .PP
146 The
147 .BR free ()
148 function returns no value.
149 .PP
150 The
151 .BR realloc ()
152 function returns a pointer to the newly allocated memory, which is suitably
153 aligned for any kind of variable and may be different from
154 .IR ptr ,
155 or NULL if the request fails.
156 If
157 .I size
158 was equal to 0, either NULL or a pointer suitable to be passed to
159 .BR free ()
160 is returned.
161 If
162 .BR realloc ()
163 fails the original block is left untouched; it is not freed or moved.
164 .SH "CONFORMING TO"
165 C89, C99.
166 .SH NOTES
167 By default, Linux follows an optimistic memory allocation strategy.
168 This means that when
169 .BR malloc ()
170 returns non-NULL there is no guarantee that the memory really
171 is available.
172 In case it turns out that the system is out of memory,
173 one or more processes will be killed by the OOM killer.
174 For more information, see the description of
175 .IR /proc/sys/vm/overcommit_memory
176 and
177 .IR /proc/sys/vm/oom_adj
178 in
179 .BR proc (5),
180 and the kernel source file
181 .IR Documentation/vm/overcommit-accounting .
182
183 Normally,
184 .BR malloc ()
185 allocates memory from the heap, and adjusts the size of the heap
186 as required, using
187 .BR sbrk (2).
188 When allocating blocks of memory larger than
189 .B MMAP_THRESHOLD
190 bytes, the glibc
191 .BR malloc ()
192 implementation allocates the memory as a private anonymous mapping using
193 .BR mmap (2).
194 .B MMAP_THRESHOLD
195 is 128 kB by default, but is adjustable using
196 .BR mallopt (3).
197 .\" FIXME . there is no mallopt(3) man page yet.
198 Allocations performed using
199 .BR mmap (2)
200 are unaffected by the
201 .B RLIMIT_DATA
202 resource limit (see
203 .BR getrlimit (2)).
204
205 The UNIX 98 standard requires
206 .BR malloc (),
207 .BR calloc (),
208 and
209 .BR realloc ()
210 to set
211 .I errno
212 to
213 .B ENOMEM
214 upon failure.
215 Glibc assumes that this is done
216 (and the glibc versions of these routines do this); if you
217 use a private malloc implementation that does not set
218 .IR errno ,
219 then certain library routines may fail without having
220 a reason in
221 .IR errno .
222 .LP
223 Crashes in
224 .BR malloc (),
225 .BR calloc (),
226 .BR realloc (),
227 or
228 .BR free ()
229 are almost always related to heap corruption, such as overflowing
230 an allocated chunk or freeing the same pointer twice.
231 .PP
232 Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
233 include a
234 .BR malloc ()
235 implementation which is tunable via environment variables.
236 When
237 .B MALLOC_CHECK_
238 is set, a special (less efficient) implementation is used which
239 is designed to be tolerant against simple errors, such as double
240 calls of
241 .BR free ()
242 with the same argument, or overruns of a single byte (off-by-one
243 bugs).
244 Not all such errors can be protected against, however, and
245 memory leaks can result.
246 If
247 .B MALLOC_CHECK_
248 is set to 0, any detected heap corruption is silently ignored;
249 if set to 1, a diagnostic message is printed on \fIstderr\fP;
250 if set to 2,
251 .BR abort (3)
252 is called immediately;
253 if set to 3, a diagnostic message is printed on \fIstderr\fP
254 and the program is aborted.
255 Using a nonzero
256 .B MALLOC_CHECK_
257 value can be useful because otherwise
258 a crash may happen much later, and the true cause for the problem
259 is then very hard to track down.
260 .SH "SEE ALSO"
261 .BR brk (2),
262 .\" .BR mallopt (3),
263 .BR mmap (2),
264 .BR alloca (3),
265 .BR posix_memalign (3)