1 .\" Copyright (c) 1980, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the American National Standards Committee X3, on Information
6 .\" Processing Systems.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
17 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 .\" $FreeBSD: head/lib/libc/stdlib/reallocarray.3 282472 2015-05-05 10:44:17Z pluknet $
37 .Nd memory reallocation functions
43 .Fn reallocarray "void *ptr" "size_t number" "size_t size"
45 .Fn recallocarray "void *ptr" "size_t oldnmemb" "size_t nmemb" "size_t size"
49 function is similar to the
51 function except it operates on
55 and checks for integer overflow in the calculation
62 function is similar to
64 except it ensures newly allocated memory is cleared similar to
69 function returns a pointer to the allocated space; otherwise, a
71 pointer is returned and
78 when there is multiplication in the
84 For example, avoid this common idiom as it may lead to integer overflow:
85 .Bd -literal -offset indent
86 if ((p = malloc(num * size)) == NULL)
90 A drop-in replacement is the
94 .Bd -literal -offset indent
95 if ((p = reallocarray(NULL, num, size)) == NULL)
96 err(1, "reallocarray");
101 be careful to avoid the following idiom:
102 .Bd -literal -offset indent
104 if ((p = realloc(p, size)) == NULL)
108 Do not adjust the variable describing how much memory has been allocated
109 until the allocation has been successful.
110 This can cause aberrant program behavior if the incorrect size value is used.
111 In most cases, the above sample will also result in a leak of memory.
112 As stated earlier, a return value of
114 indicates that the old object still remains allocated.
115 Better code looks like this:
116 .Bd -literal -offset indent
118 if ((newp = realloc(p, newsize)) == NULL) {
130 it is important to ensure the new size value will not overflow;
131 i.e. avoid allocations like the following:
132 .Bd -literal -offset indent
133 if ((newp = realloc(p, num * size)) == NULL) {
139 .Bd -literal -offset indent
140 if ((newp = reallocarray(p, num, size)) == NULL) {
148 function first appeared in