]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/malloc.3
fanotify_init.2, fanotify.7: Document FAN_REPORT_TID
[thirdparty/man-pages.git] / man3 / malloc.3
CommitLineData
bf5a7247 1.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
fea681da 2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 12.\"
fea681da
MK
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.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
c08df37a 24.\"
fea681da
MK
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)
005d4448 28.\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
fea681da 29.\"
bea08fec
MK
30.\" FIXME . Review http://austingroupbugs.net/view.php?id=374
31.\" to see what changes are required on this page.
7c5ca666 32.\"
4b8c67d9 33.TH MALLOC 3 2017-09-15 "GNU" "Linux Programmer's Manual"
fea681da 34.SH NAME
f68512e9 35malloc, free, calloc, realloc \- allocate and free dynamic memory
fea681da
MK
36.SH SYNOPSIS
37.nf
38.B #include <stdlib.h>
68e4db0a 39.PP
fea681da 40.BI "void *malloc(size_t " "size" );
fea681da 41.BI "void free(void " "*ptr" );
12ec54b4 42.BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
d9ec0c6a 43.BI "void *realloc(void " "*ptr" ", size_t " "size" );
081eeee5 44.BI "void *reallocarray(void " "*ptr" ", size_t " nmemb ", size_t " "size" );
fea681da 45.fi
081eeee5
MK
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 ():
081eeee5
MK
53.ad l
54_GNU_SOURCE
55.RE
d8d70100
MK
56 Since glibc 2.29:
57 _DEFAULT_SOURCE
58 Glibc 2.28 and earlier:
59 _GNU_SOURCE
081eeee5 60.ad
fea681da 61.SH DESCRIPTION
fea681da 62.PP
12ec54b4 63The
63aa9df0 64.BR malloc ()
12ec54b4 65function allocates
fea681da 66.I size
c13182ef 67bytes and returns a pointer to the allocated memory.
655d270c 68.IR "The memory is not initialized" .
840b4581
MK
69If
70.I size
bf1c0ede 71is 0, then
840b4581
MK
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 ().
fea681da 77.PP
12ec54b4 78The
63aa9df0 79.BR free ()
12ec54b4 80function frees the memory space pointed to by
fea681da
MK
81.IR ptr ,
82which must have been returned by a previous call to
63aa9df0 83.BR malloc (),
c8624d68 84.BR calloc (),
fea681da 85or
63aa9df0 86.BR realloc ().
fea681da 87Otherwise, or if
0daa9e92 88.I free(ptr)
d9bfdb9c 89has already been called before, undefined behavior occurs.
fea681da
MK
90If
91.I ptr
8478ee02 92is NULL, no operation is performed.
fea681da 93.PP
12ec54b4
MK
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 ().
b7b0f189
MK
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
12ec54b4
MK
129.PP
130The
63aa9df0 131.BR realloc ()
12ec54b4 132function changes the size of the memory block pointed to by
fea681da
MK
133.I ptr
134to
135.I size
136bytes.
b4b57a95 137The contents will be unchanged in the range from the start of the region
655d270c
MK
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.
fea681da
MK
142If
143.I ptr
c4acc689 144is NULL, then the call is equivalent to
865c9fd8
MK
145.IR malloc(size) ,
146for all values of
147.IR size ;
c13182ef
MK
148if
149.I size
054fccc0 150is equal to zero,
c4acc689
MK
151and
152.I ptr
153is not NULL, then the call is equivalent to
840b4581 154.IR free(ptr) .
fea681da
MK
155Unless
156.I ptr
8478ee02 157is NULL, it must have been returned by an earlier call to
63aa9df0 158.BR malloc (),
bc045fdd 159.BR calloc (),
fea681da 160or
63aa9df0 161.BR realloc ().
fea681da 162If the area pointed to was moved, a
0daa9e92 163.I free(ptr)
fea681da 164is done.
081eeee5
MK
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.
47297adb 193.SH RETURN VALUE
12ec54b4
MK
194The
195.BR malloc ()
c13182ef 196and
12ec54b4 197.BR calloc ()
25630b27
GP
198functions return a pointer to the allocated memory,
199which is suitably aligned for any built-in type.
fdc6b831
MK
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