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