xref: /freebsd/lib/libc/stdlib/reallocarray.3 (revision 046c625e9382e17da953767b881aaa782fa73af8)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
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.
16.\"
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
27.\" SUCH DAMAGE.
28.\"
29.Dd October 2, 2025
30.Dt REALLOCARRAY 3
31.Os
32.Sh NAME
33.Nm reallocarray
34.Nd memory reallocation function
35.Sh LIBRARY
36.Lb libc
37.Sh SYNOPSIS
38.In stdlib.h
39.Ft void *
40.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size"
41.Ft void *
42.Fn recallocarray "void *ptr" "size_t oldnmeb" "size_t nmemb" size_t size"
43.Sh DESCRIPTION
44The
45.Fn reallocarray
46function is similar to the
47.Fn realloc
48function
49except it operates on
50.Fa nmemb
51members of size
52.Fa size
53and checks for integer overflow in the calculation
54.Fa nmemb
55*
56.Fa size .
57.Pp
58The
59.Fn recallocarray
60function is similar to the
61.Fn reallocarray
62function
63except it ensures newly allocated memory is cleared similar to
64.Fn calloc .
65If
66.Fa ptr
67is
68.Dv NULL ,
69.Fa oldnmemb
70is ignored and the call	is equivalent to
71.Fn calloc .
72If
73.Fa ptr
74is not
75.Dv NULL ,
76.Fa oldnmemb
77must be a value such that
78.Fa oldnmemb
79*
80.Fa size
81is the size of the earlier allocation that returned
82.Fa ptr ,
83otherwise the behaviour is undefined.
84.Sh RETURN VALUES
85The
86.Fn reallocarray
87function returns a pointer to the allocated space; otherwise, a
88.Dv NULL
89pointer is returned and
90.Va errno
91is set to
92.Er ENOMEM .
93.Sh EXAMPLES
94Consider
95.Fn reallocarray
96when there is multiplication in the
97.Fa size
98argument of
99.Fn malloc
100or
101.Fn realloc .
102For example, avoid this common idiom as it may lead to integer overflow:
103.Bd -literal -offset indent
104if ((p = malloc(num * size)) == NULL)
105	err(1, "malloc");
106.Ed
107.Pp
108A drop-in replacement is the
109.Ox
110extension
111.Fn reallocarray :
112.Bd -literal -offset indent
113if ((p = reallocarray(NULL, num, size)) == NULL)
114	err(1, "reallocarray");
115.Ed
116.Pp
117When using
118.Fn realloc ,
119be careful to avoid the following idiom:
120.Bd -literal -offset indent
121size += 50;
122if ((p = realloc(p, size)) == NULL)
123	return (NULL);
124.Ed
125.Pp
126Do not adjust the variable describing how much memory has been allocated
127until the allocation has been successful.
128This can cause aberrant program behavior if the incorrect size value is used.
129In most cases, the above sample will also result in a leak of memory.
130As stated earlier, a return value of
131.Dv NULL
132indicates that the old object still remains allocated.
133Better code looks like this:
134.Bd -literal -offset indent
135newsize = size + 50;
136if ((newp = realloc(p, newsize)) == NULL) {
137	free(p);
138	p = NULL;
139	size = 0;
140	return (NULL);
141}
142p = newp;
143size = newsize;
144.Ed
145.Pp
146As with
147.Fn malloc ,
148it is important to ensure the new size value will not overflow;
149i.e. avoid allocations like the following:
150.Bd -literal -offset indent
151if ((newp = realloc(p, num * size)) == NULL) {
152	...
153.Ed
154.Pp
155Instead, use
156.Fn reallocarray :
157.Bd -literal -offset indent
158if ((newp = reallocarray(p, num, size)) == NULL) {
159	...
160.Ed
161.Sh SEE ALSO
162.Xr realloc 3
163.Sh STANDARDS
164.Fn reallocarray
165conforms to
166.St -p1003.1-2024 .
167.Sh HISTORY
168The
169.Fn reallocarray
170function first appeared in
171.Ox 5.6
172and
173.Fx 11.0 .
174The
175.Fn recallocarray
176function first appeared in
177.Ox 6.1
178and
179.Fx 16.0 .
180