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