OSDN Git Service

5f482f379a9443ed041198688ca0f98f5355adb8
[linuxjm/LDP_man-pages.git] / original / man3 / malloc.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
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.
12 .\"
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.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
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)
28 .\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
29 .\"
30 .TH MALLOC 3  2013-12-12 "GNU" "Linux Programmer's Manual"
31 .SH NAME
32 malloc, free, calloc, realloc \- allocate and free dynamic memory
33 .SH SYNOPSIS
34 .nf
35 .B #include <stdlib.h>
36 .sp
37 .BI "void *malloc(size_t " "size" );
38 .BI "void free(void " "*ptr" );
39 .BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
40 .BI "void *realloc(void " "*ptr" ", size_t "  "size" );
41 .fi
42 .SH DESCRIPTION
43 .PP
44 The
45 .BR malloc ()
46 function allocates
47 .I size
48 bytes and returns a pointer to the allocated memory.
49 .IR "The memory is not initialized" .
50 If
51 .I size
52 is 0, then
53 .BR malloc ()
54 returns either NULL,
55 .\" glibc does this:
56 or a unique pointer value that can later be successfully passed to
57 .BR free ().
58 .PP
59 The
60 .BR free ()
61 function frees the memory space pointed to by
62 .IR ptr ,
63 which must have been returned by a previous call to
64 .BR malloc (),
65 .BR calloc ()
66 or
67 .BR realloc ().
68 Otherwise, or if
69 .I free(ptr)
70 has already been called before, undefined behavior occurs.
71 If
72 .I ptr
73 is NULL, no operation is performed.
74 .PP
75 The
76 .BR calloc ()
77 function allocates memory for an array of
78 .I nmemb
79 elements of
80 .I size
81 bytes each and returns a pointer to the allocated memory.
82 The memory is set to zero.
83 If
84 .I nmemb
85 or
86 .I size
87 is 0, then
88 .BR calloc ()
89 returns either NULL,
90 .\" glibc does this:
91 or a unique pointer value that can later be successfully passed to
92 .BR free ().
93 .PP
94 The
95 .BR realloc ()
96 function changes the size of the memory block pointed to by
97 .I ptr
98 to
99 .I size
100 bytes.
101 The contents will be unchanged in the range from the start of the region
102 up to the minimum of the old and new sizes.
103 If the new size is larger than the old size, the added memory will
104 .I not
105 be initialized.
106 If
107 .I ptr
108 is NULL, then the call is equivalent to
109 .IR malloc(size) ,
110 for all values of
111 .IR size ;
112 if
113 .I size
114 is equal to zero,
115 and
116 .I ptr
117 is not NULL, then the call is equivalent to
118 .IR free(ptr) .
119 Unless
120 .I ptr
121 is NULL, it must have been returned by an earlier call to
122 .BR malloc (),
123 .BR calloc ()
124 or
125 .BR realloc ().
126 If the area pointed to was moved, a
127 .I free(ptr)
128 is done.
129 .SH RETURN VALUE
130 The
131 .BR malloc ()
132 and
133 .BR calloc ()
134 functions return a pointer to the allocated memory,
135 which is suitably aligned for any built-in type.
136 On error, these functions return NULL.
137 NULL may also be returned by a successful call to
138 .BR malloc ()
139 with a
140 .I size
141 of zero,
142 or by a successful call to
143 .BR calloc ()
144 with
145 .I nmemb
146 or
147 .I size
148 equal to zero.
149 .PP
150 The
151 .BR free ()
152 function returns no value.
153 .PP
154 The
155 .BR realloc ()
156 function returns a pointer to the newly allocated memory, which is suitably
157 aligned for any built-in type and may be different from
158 .IR ptr ,
159 or NULL if the request fails.
160 If
161 .I size
162 was equal to 0, either NULL or a pointer suitable to be passed to
163 .BR free ()
164 is returned.
165 If
166 .BR realloc ()
167 fails, the original block is left untouched; it is not freed or moved.
168 .SH CONFORMING TO
169 C89, C99.
170 .SH NOTES
171 By default, Linux follows an optimistic memory allocation strategy.
172 This means that when
173 .BR malloc ()
174 returns non-NULL there is no guarantee that the memory really
175 is available.
176 In case it turns out that the system is out of memory,
177 one or more processes will be killed by the OOM killer.
178 For more information, see the description of
179 .IR /proc/sys/vm/overcommit_memory
180 and
181 .IR /proc/sys/vm/oom_adj
182 in
183 .BR proc (5),
184 and the Linux kernel source file
185 .IR Documentation/vm/overcommit-accounting .
186
187 Normally,
188 .BR malloc ()
189 allocates memory from the heap, and adjusts the size of the heap
190 as required, using
191 .BR sbrk (2).
192 When allocating blocks of memory larger than
193 .B MMAP_THRESHOLD
194 bytes, the glibc
195 .BR malloc ()
196 implementation allocates the memory as a private anonymous mapping using
197 .BR mmap (2).
198 .B MMAP_THRESHOLD
199 is 128 kB by default, but is adjustable using
200 .BR mallopt (3).
201 Allocations performed using
202 .BR mmap (2)
203 are unaffected by the
204 .B RLIMIT_DATA
205 resource limit (see
206 .BR getrlimit (2)).
207
208 To avoid corruption in multithreaded applications,
209 mutexes are used internally to protect the memory-management
210 data structures employed by these functions.
211 In a multithreaded application in which threads simultaneously
212 allocate and free memory,
213 there could be contention for these mutexes.
214 To scalably handle memory allocation in multithreaded applications,
215 glibc creates additional
216 .IR "memory allocation arenas"
217 if mutex contention is detected.
218 Each arena is a large region of memory that is internally allocated
219 by the system
220 (using
221 .BR brk (2)
222 or
223 .BR mmap (2)),
224 and managed with its own mutexes.
225
226 The UNIX 98 standard requires
227 .BR malloc (),
228 .BR calloc (),
229 and
230 .BR realloc ()
231 to set
232 .I errno
233 to
234 .B ENOMEM
235 upon failure.
236 Glibc assumes that this is done
237 (and the glibc versions of these routines do this); if you
238 use a private malloc implementation that does not set
239 .IR errno ,
240 then certain library routines may fail without having
241 a reason in
242 .IR errno .
243 .LP
244 Crashes in
245 .BR malloc (),
246 .BR calloc (),
247 .BR realloc (),
248 or
249 .BR free ()
250 are almost always related to heap corruption, such as overflowing
251 an allocated chunk or freeing the same pointer twice.
252 .PP
253 Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
254 include a
255 .BR malloc ()
256 implementation which is tunable via environment variables.
257 For details, see
258 .BR mallopt (3).
259 .SH SEE ALSO
260 .\" http://g.oswego.edu/dl/html/malloc.html
261 .\" A Memory Allocator - by Doug Lea
262 .\"
263 .\" http://www.bozemanpass.com/info/linux/malloc/Linux_Heap_Contention.html
264 .\" Linux Heap, Contention in free() - David Boreham
265 .\"
266 .\" http://www.citi.umich.edu/projects/linux-scalability/reports/malloc.html
267 .\" malloc() Performance in a Multithreaded Linux Environment -
268 .\"     Check Lever, David Boreham
269 .\"
270 .ad l
271 .nh
272 .BR brk (2),
273 .BR mmap (2),
274 .BR alloca (3),
275 .BR malloc_get_state (3),
276 .BR malloc_info (3),
277 .BR malloc_trim (3),
278 .BR malloc_usable_size (3),
279 .BR mallopt (3),
280 .BR mcheck (3),
281 .BR mtrace (3),
282 .BR posix_memalign (3)
283 .SH COLOPHON
284 This page is part of release 3.64 of the Linux
285 .I man-pages
286 project.
287 A description of the project,
288 and information about reporting bugs,
289 can be found at
290 \%http://www.kernel.org/doc/man\-pages/.