1 .\" $NetBSD: malloc.3,v 1.38 2010/05/03 08:23:20 jruoho Exp $
3 .\" Copyright (c) 1980, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
7 .\" the American National Standards Committee X3, on Information
8 .\" Processing Systems.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in the
17 .\" documentation and/or other materials provided with the distribution.
18 .\" 3. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
35 .\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.73 2007/06/15 22:32:33 jasone Exp $
41 .Nm malloc , calloc , realloc , free
42 .Nd general purpose memory allocation functions
48 .Fn malloc "size_t size"
50 .Fn calloc "size_t number" "size_t size"
52 .Fn realloc "void *ptr" "size_t size"
60 bytes of uninitialized memory.
61 The allocated space is suitably aligned (after possible pointer coercion)
62 for storage of any type of object.
66 function allocates space for
72 The result is identical to calling
76 with the exception that the allocated memory is explicitly initialized
81 function changes the size of the previously allocated memory referenced by
86 The contents of the memory are unchanged up to the lesser of the new and
88 If the new size is larger,
89 the value of the newly allocated portion of the memory is undefined.
90 Upon success, the memory referenced by
92 is freed and a pointer to the newly allocated memory is returned.
95 may move the memory allocation, resulting in a different return value than
103 function behaves identically to
105 for the specified size.
109 function causes the allocated memory referenced by
111 to be made available for future allocations.
122 functions return a pointer to the allocated memory if successful; otherwise
125 pointer is returned and
132 function returns a pointer, possibly identical to
134 to the allocated memory
135 if successful; otherwise a
137 pointer is returned, and
141 if the error was the result of an allocation failure.
144 function always leaves the original buffer intact
145 when an error occurs.
149 function returns no value.
153 be careful to avoid the following idiom:
154 .Bd -literal -offset indent
155 if ((p = malloc(number * size)) == NULL)
156 err(EXIT_FAILURE, "malloc");
159 The multiplication may lead to an integer overflow.
166 must be used, be sure to test for overflow:
167 .Bd -literal -offset indent
168 if (size && number > SIZE_MAX / size) {
170 err(EXIT_FAILURE, "allocation");
176 one must be careful to avoid the following idiom:
178 .Bd -literal -offset indent
181 if ((p = realloc(p, nsize)) == NULL)
185 Do not adjust the variable describing how much memory has been allocated
186 until it is known that the allocation has been successful.
187 This can cause aberrant program behavior if the incorrect size value is used.
188 In most cases, the above example will also leak memory.
189 As stated earlier, a return value of
191 indicates that the old object still remains allocated.
192 Better code looks like this:
193 .Bd -literal -offset indent
196 if ((p2 = realloc(p, newsize)) == NULL) {
219 For the implementation details, see