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