]> git.ipfire.org Git - thirdparty/man-pages.git/blame_incremental - man3/malloc.3
fanotify_init.2, fanotify.7: Document FAN_REPORT_TID
[thirdparty/man-pages.git] / man3 / malloc.3
... / ...
CommitLineData
1.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2.\"
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.
7.\"
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.
12.\"
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
19.\" professionally.
20.\"
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\" %%%LICENSE_END
24.\"
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().
29.\"
30.\" FIXME . Review http://austingroupbugs.net/view.php?id=374
31.\" to see what changes are required on this page.
32.\"
33.TH MALLOC 3 2017-09-15 "GNU" "Linux Programmer's Manual"
34.SH NAME
35malloc, free, calloc, realloc \- allocate and free dynamic memory
36.SH SYNOPSIS
37.nf
38.B #include <stdlib.h>
39.PP
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" );
45.fi
46.PP
47.in -4n
48Feature Test Macro Requirements for glibc (see
49.BR feature_test_macros (7)):
50.in
51.PP
52.BR reallocarray ():
53.ad l
54_GNU_SOURCE
55.RE
56 Since glibc 2.29:
57 _DEFAULT_SOURCE
58 Glibc 2.28 and earlier:
59 _GNU_SOURCE
60.ad
61.SH DESCRIPTION
62.PP
63The
64.BR malloc ()
65function allocates
66.I size
67bytes and returns a pointer to the allocated memory.
68.IR "The memory is not initialized" .
69If
70.I size
71is 0, then
72.BR malloc ()
73returns either NULL,
74.\" glibc does this:
75or a unique pointer value that can later be successfully passed to
76.BR free ().
77.PP
78The
79.BR free ()
80function frees the memory space pointed to by
81.IR ptr ,
82which must have been returned by a previous call to
83.BR malloc (),
84.BR calloc (),
85or
86.BR realloc ().
87Otherwise, or if
88.I free(ptr)
89has already been called before, undefined behavior occurs.
90If
91.I ptr
92is NULL, no operation is performed.
93.PP
94The
95.BR calloc ()
96function allocates memory for an array of
97.I nmemb
98elements of
99.I size
100bytes each and returns a pointer to the allocated memory.
101The memory is set to zero.
102If
103.I nmemb
104or
105.I size
106is 0, then
107.BR calloc ()
108returns either NULL,
109.\" glibc does this:
110or a unique pointer value that can later be successfully passed to
111.BR free ().
112If the multiplication of
113.I nmemb
114and
115.I size
116would result in integer overflow, then
117.BR calloc ()
118returns an error.
119By contrast,
120an integer overflow would not be detected in the following call to
121.BR malloc (),
122with the result that an incorrectly sized block of memory would be allocated:
123.PP
124.in +4n
125.EX
126malloc(nmemb * size);
127.EE
128.in
129.PP
130The
131.BR realloc ()
132function changes the size of the memory block pointed to by
133.I ptr
134to
135.I size
136bytes.
137The contents will be unchanged in the range from the start of the region
138up to the minimum of the old and new sizes.
139If the new size is larger than the old size, the added memory will
140.I not
141be initialized.
142If
143.I ptr
144is NULL, then the call is equivalent to
145.IR malloc(size) ,
146for all values of
147.IR size ;
148if
149.I size
150is equal to zero,
151and
152.I ptr
153is not NULL, then the call is equivalent to
154.IR free(ptr) .
155Unless
156.I ptr
157is NULL, it must have been returned by an earlier call to
158.BR malloc (),
159.BR calloc (),
160or
161.BR realloc ().
162If the area pointed to was moved, a
163.I free(ptr)
164is done.
165.PP
166The
167.BR reallocarray ()
168function changes the size of the memory block pointed to by
169.I ptr
170to be large enough for an array of
171.I nmemb
172elements, each of which is
173.I size
174bytes.
175It is equivalent to the call
176.PP
177.in +4n
178 realloc(ptr, nmemb * size);
179.in
180.PP
181However, unlike that
182.BR realloc ()
183call,
184.BR reallocarray ()
185fails safely in the case where the multiplication would overflow.
186If such an overflow occurs,
187.BR reallocarray ()
188returns NULL, sets
189.I errno
190to
191.BR ENOMEM ,
192and leaves the original block of memory unchanged.
193.SH RETURN VALUE
194The
195.BR malloc ()
196and
197.BR calloc ()
198functions return a pointer to the allocated memory,
199which is suitably aligned for any built-in type.
200On error, these functions return NULL.
201NULL may also be returned by a successful call to
202.BR malloc ()
203with a
204.I size
205of zero,
206or by a successful call to
207.BR calloc ()
208with
209.I nmemb
210or
211.I size
212equal to zero.
213.PP
214The
215.BR free ()
216function returns no value.
217.PP
218The
219.BR realloc ()
220function returns a pointer to the newly allocated memory, which is suitably
221aligned for any built-in type and may be different from
222.IR ptr ,
223or NULL if the request fails.
224If
225.I size
226was equal to 0, either NULL or a pointer suitable to be passed to
227.BR free ()
228is returned.
229If
230.BR realloc ()
231fails, the original block is left untouched; it is not freed or moved.
232.PP
233On success, the
234.BR reallocarray ()
235function returns a pointer to the newly allocated memory.
236On failure,
237it returns NULL and the original block of memory is left untouched.
238.SH ERRORS
239.BR calloc (),
240.BR malloc (),
241.BR realloc (),
242and
243.BR reallocarray ()
244can fail with the following error:
245.TP
246.B ENOMEM
247Out of memory.
248Possibly, the application hit the
249.BR RLIMIT_AS
250or
251.BR RLIMIT_DATA
252limit described in
253.BR getrlimit (2).
254.SH ATTRIBUTES
255For an explanation of the terms used in this section, see
256.BR attributes (7).
257.TS
258allbox;
259lbw20 lb lb
260l l l.
261Interface Attribute Value
262T{
263.BR malloc (),
264.BR free (),
265.br
266.BR calloc (),
267.BR realloc ()
268T} Thread safety MT-Safe
269.TE
270.SH CONFORMING TO
271.BR malloc (),
272.BR free (),
273.BR calloc (),
274.BR realloc ():
275POSIX.1-2001, POSIX.1-2008, C89, C99.
276.PP
277.BR reallocarray ()
278is a nonstandard extension that first appeared in OpenBSD 5.6 and FreeBSD 11.0.
279.SH NOTES
280By default, Linux follows an optimistic memory allocation strategy.
281This means that when
282.BR malloc ()
283returns non-NULL there is no guarantee that the memory really
284is available.
285In case it turns out that the system is out of memory,
286one or more processes will be killed by the OOM killer.
287For more information, see the description of
288.IR /proc/sys/vm/overcommit_memory
289and
290.IR /proc/sys/vm/oom_adj
291in
292.BR proc (5),
293and the Linux kernel source file
294.IR Documentation/vm/overcommit-accounting.rst .
295.PP
296Normally,
297.BR malloc ()
298allocates memory from the heap, and adjusts the size of the heap
299as required, using
300.BR sbrk (2).
301When allocating blocks of memory larger than
302.B MMAP_THRESHOLD
303bytes, the glibc
304.BR malloc ()
305implementation allocates the memory as a private anonymous mapping using
306.BR mmap (2).
307.B MMAP_THRESHOLD
308is 128\ kB by default, but is adjustable using
309.BR mallopt (3).
310Prior to Linux 4.7
311allocations performed using
312.BR mmap (2)
313were unaffected by the
314.B RLIMIT_DATA
315resource limit;
316since Linux 4.7, this limit is also enforced for allocations performed using
317.BR mmap (2).
318.PP
319To avoid corruption in multithreaded applications,
320mutexes are used internally to protect the memory-management
321data structures employed by these functions.
322In a multithreaded application in which threads simultaneously
323allocate and free memory,
324there could be contention for these mutexes.
325To scalably handle memory allocation in multithreaded applications,
326glibc creates additional
327.IR "memory allocation arenas"
328if mutex contention is detected.
329Each arena is a large region of memory that is internally allocated
330by the system
331(using
332.BR brk (2)
333or
334.BR mmap (2)),
335and managed with its own mutexes.
336.PP
337SUSv2 requires
338.BR malloc (),
339.BR calloc (),
340and
341.BR realloc ()
342to set
343.I errno
344to
345.B ENOMEM
346upon failure.
347Glibc assumes that this is done
348(and the glibc versions of these routines do this); if you
349use a private malloc implementation that does not set
350.IR errno ,
351then certain library routines may fail without having
352a reason in
353.IR errno .
354.PP
355Crashes in
356.BR malloc (),
357.BR calloc (),
358.BR realloc (),
359or
360.BR free ()
361are almost always related to heap corruption, such as overflowing
362an allocated chunk or freeing the same pointer twice.
363.PP
364The
365.BR malloc ()
366implementation is tunable via environment variables; see
367.BR mallopt (3)
368for details.
369.SH SEE ALSO
370.\" http://g.oswego.edu/dl/html/malloc.html
371.\" A Memory Allocator - by Doug Lea
372.\"
373.\" http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html
374.\" Linux Heap, Contention in free() - David Boreham
375.\"
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
379.\"
380.ad l
381.nh
382.BR valgrind (1),
383.BR brk (2),
384.BR mmap (2),
385.BR alloca (3),
386.BR malloc_get_state (3),
387.BR malloc_info (3),
388.BR malloc_trim (3),
389.BR malloc_usable_size (3),
390.BR mallopt (3),
391.BR mcheck (3),
392.BR mtrace (3),
393.BR posix_memalign (3)
394.PP
395For details of the GNU C library implementation, see
396.UR https://sourceware.org/glibc/wiki/MallocInternals
397.UE .