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