]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/malloc.3
Change itacised function names and page xrefs to bold
[thirdparty/man-pages.git] / man3 / malloc.3
CommitLineData
fea681da
MK
1.\" (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.\"
27.TH MALLOC 3 1993-04-04 "GNU" "Linux Programmer's Manual"
28.SH NAME
29calloc, malloc, free, realloc \- Allocate and free dynamic memory
30.SH SYNOPSIS
31.nf
32.B #include <stdlib.h>
33.sp
34.BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
35.nl
36.BI "void *malloc(size_t " "size" );
37.nl
38.BI "void free(void " "*ptr" );
39.nl
40.BI "void *realloc(void " "*ptr" ", size_t " "size" );
41.fi
42.SH DESCRIPTION
63aa9df0 43.BR calloc ()
fea681da
MK
44allocates memory for an array of
45.I nmemb
46elements of
47.I size
48bytes each and returns a pointer to the allocated memory.
49The memory is set to zero.
50.PP
63aa9df0 51.BR malloc ()
fea681da
MK
52allocates
53.I size
54bytes and returns a pointer to the allocated memory.
55The memory is not cleared.
56.PP
63aa9df0 57.BR free ()
fea681da
MK
58frees the memory space pointed to by
59.IR ptr ,
60which must have been returned by a previous call to
63aa9df0
MK
61.BR malloc (),
62.BR calloc ()
fea681da 63or
63aa9df0 64.BR realloc ().
fea681da
MK
65Otherwise, or if
66.BI "free(" "ptr" )
67has already been called before, undefined behaviour occurs.
68If
69.I ptr
70is
71.BR NULL ,
72no operation is performed.
73.PP
63aa9df0 74.BR realloc ()
fea681da
MK
75changes the size of the memory block pointed to by
76.I ptr
77to
78.I size
79bytes.
80The contents will be unchanged to the minimum of the old and new sizes;
81newly allocated memory will be uninitialized.
82If
83.I ptr
84is
85.BR NULL ,
86the call is equivalent to
87.BR malloc(size) ;
88if size is equal to zero,
89the call is equivalent to
90.BI "free(" "ptr" ) .
91Unless
92.I ptr
93is
94.BR NULL ,
95it must have been returned by an earlier call to
63aa9df0
MK
96.BR malloc (),
97.BR calloc ()
fea681da 98or
63aa9df0 99.BR realloc ().
fea681da
MK
100If the area pointed to was moved, a
101.BI "free(" "ptr" )
102is done.
103.SH "RETURN VALUE"
104For
e1d6264d
MK
105.BR calloc ()
106and
107.BR malloc (),
fea681da
MK
108the value returned is a pointer to the allocated memory, which is suitably
109aligned for any kind of variable, or
110.B NULL
111if the request fails.
112.PP
63aa9df0 113.BR free ()
fea681da
MK
114returns no value.
115.PP
63aa9df0 116.BR realloc ()
fea681da
MK
117returns a pointer to the newly allocated memory, which is suitably
118aligned for any kind of variable and may be different from
119.IR ptr ,
120or
121.B NULL
122if the request fails. If
123.I size
124was equal to 0, either NULL or a pointer suitable to be passed to
31e9a9ec 125.BR free ()
fea681da 126is returned. If
63aa9df0 127.BR realloc ()
c65433e6 128fails the original block is left untouched; it is not freed or moved.
fea681da
MK
129.SH "CONFORMING TO"
130ANSI-C
131.SH "SEE ALSO"
132.BR brk (2),
133.BR posix_memalign (3)
134.SH NOTES
135The Unix98 standard requires
63aa9df0
MK
136.BR malloc (),
137.BR calloc (),
fea681da
MK
138and
139.BR realloc ()
140to set
141.I errno
142to ENOMEM upon failure. Glibc assumes that this is done
143(and the glibc versions of these routines do this); if you
144use a private malloc implementation that does not set
145.IR errno ,
146then certain library routines may fail without having
147a reason in
148.IR errno .
149.LP
150Crashes in
63aa9df0
MK
151.BR malloc (),
152.BR free ()
fea681da 153or
63aa9df0 154.BR realloc ()
fea681da
MK
155are almost always related to heap corruption, such as overflowing
156an allocated chunk or freeing the same pointer twice.
157.PP
158Recent versions of Linux libc (later than 5.4.23) and GNU libc (2.x)
159include a malloc implementation which is tunable via environment
160variables. When
161.BR MALLOC_CHECK_
162is set, a special (less efficient) implementation is used which
163is designed to be tolerant against simple errors, such as double
164calls of
63aa9df0 165.BR free ()
fea681da
MK
166with the same argument, or overruns of a single byte (off-by-one
167bugs). Not all such errors can be protected against, however, and
168memory leaks can result.
169If
170.BR MALLOC_CHECK_
171is set to 0, any detected heap corruption is silently ignored;
172if set to 1, a diagnostic is printed on stderr;
173if set to 2,
63aa9df0 174.BR abort ()
fea681da
MK
175is called immediately. This can be useful because otherwise
176a crash may happen much later, and the true cause for the problem
177is then very hard to track down.
178.SH BUGS
179By default, Linux follows an optimistic memory allocation strategy.
180This means that when
63aa9df0 181.BR malloc ()
fea681da
MK
182returns non-NULL there is no guarantee that the memory really
183is available. This is a really bad bug.
184In case it turns out that the system is out of memory,
185one or more processes will be killed by the infamous OOM killer.
186In case Linux is employed under circumstances where it would be
187less desirable to suddenly lose some randomly picked processes,
188and moreover the kernel version is sufficiently recent,
189one can switch off this overcommitting behavior using a command like
190.RS
191# echo 2 > /proc/sys/vm/overcommit_memory
192.RE
193See also the kernel Documentation directory, files
194.I vm/overcommit-accounting
195and
196.IR sysctl/vm.txt .