]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/malloc.3
capabilities.7: Ambient capabilities do not trigger secure-execution mode
[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 ():
53.br
54.RS 4
55.ad l
56_GNU_SOURCE
57.RE
58.ad
fea681da 59.SH DESCRIPTION
fea681da 60.PP
12ec54b4 61The
63aa9df0 62.BR malloc ()
12ec54b4 63function allocates
fea681da 64.I size
c13182ef 65bytes and returns a pointer to the allocated memory.
655d270c 66.IR "The memory is not initialized" .
840b4581
MK
67If
68.I size
bf1c0ede 69is 0, then
840b4581
MK
70.BR malloc ()
71returns either NULL,
72.\" glibc does this:
73or a unique pointer value that can later be successfully passed to
74.BR free ().
fea681da 75.PP
12ec54b4 76The
63aa9df0 77.BR free ()
12ec54b4 78function frees the memory space pointed to by
fea681da
MK
79.IR ptr ,
80which must have been returned by a previous call to
63aa9df0 81.BR malloc (),
c8624d68 82.BR calloc (),
fea681da 83or
63aa9df0 84.BR realloc ().
fea681da 85Otherwise, or if
0daa9e92 86.I free(ptr)
d9bfdb9c 87has already been called before, undefined behavior occurs.
fea681da
MK
88If
89.I ptr
8478ee02 90is NULL, no operation is performed.
fea681da 91.PP
12ec54b4
MK
92The
93.BR calloc ()
94function allocates memory for an array of
95.I nmemb
96elements of
97.I size
98bytes each and returns a pointer to the allocated memory.
99The memory is set to zero.
100If
101.I nmemb
102or
103.I size
104is 0, then
105.BR calloc ()
106returns either NULL,
107.\" glibc does this:
108or a unique pointer value that can later be successfully passed to
109.BR free ().
b7b0f189
MK
110If the multiplication of
111.I nmemb
112and
113.I size
114would result in integer overflow, then
115.BR calloc ()
116returns an error.
117By contrast,
118an integer overflow would not be detected in the following call to
119.BR malloc (),
120with the result that an incorrectly sized block of memory would be allocated:
121.PP
122.in +4n
123.EX
124malloc(nmemb * size);
125.EE
126.in
12ec54b4
MK
127.PP
128The
63aa9df0 129.BR realloc ()
12ec54b4 130function changes the size of the memory block pointed to by
fea681da
MK
131.I ptr
132to
133.I size
134bytes.
b4b57a95 135The contents will be unchanged in the range from the start of the region
655d270c
MK
136up to the minimum of the old and new sizes.
137If the new size is larger than the old size, the added memory will
138.I not
139be initialized.
fea681da
MK
140If
141.I ptr
c4acc689 142is NULL, then the call is equivalent to
865c9fd8
MK
143.IR malloc(size) ,
144for all values of
145.IR size ;
c13182ef
MK
146if
147.I size
054fccc0 148is equal to zero,
c4acc689
MK
149and
150.I ptr
151is not NULL, then the call is equivalent to
840b4581 152.IR free(ptr) .
fea681da
MK
153Unless
154.I ptr
8478ee02 155is NULL, it must have been returned by an earlier call to
63aa9df0 156.BR malloc (),
bc045fdd 157.BR calloc (),
fea681da 158or
63aa9df0 159.BR realloc ().
fea681da 160If the area pointed to was moved, a
0daa9e92 161.I free(ptr)
fea681da 162is done.
081eeee5
MK
163.PP
164The
165.BR reallocarray ()
166function changes the size of the memory block pointed to by
167.I ptr
168to be large enough for an array of
169.I nmemb
170elements, each of which is
171.I size
172bytes.
173It is equivalent to the call
174.PP
175.in +4n
176 realloc(ptr, nmemb * size);
177.in
178.PP
179However, unlike that
180.BR realloc ()
181call,
182.BR reallocarray ()
183fails safely in the case where the multiplication would overflow.
184If such an overflow occurs,
185.BR reallocarray ()
186returns NULL, sets
187.I errno
188to
189.BR ENOMEM ,
190and leaves the original block of memory unchanged.
47297adb 191.SH RETURN VALUE
12ec54b4
MK
192The
193.BR malloc ()
c13182ef 194and
12ec54b4 195.BR calloc ()
25630b27
GP
196functions return a pointer to the allocated memory,
197which is suitably aligned for any built-in type.
fdc6b831
MK
198On error, these functions return NULL.
199NULL may also be returned by a successful call to
200.BR malloc ()
201with a
202.I size
203of zero,
204or by a successful call to