OSDN Git Service

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