xref: /netbsd-src/lib/libc/stdlib/reallocarr.3 (revision cf221fb5729a353d47d68c690d4495b842254d10)
1*cf221fb5Sriastradh.\"	$NetBSD: reallocarr.3,v 1.7 2022/08/31 12:18:41 riastradh Exp $
2c9baa596Sjoerg.\"
3c9baa596Sjoerg.\" Copyright (c) 2015 The NetBSD Foundation, Inc.
4c9baa596Sjoerg.\" All rights reserved.
5c9baa596Sjoerg.\"
6c9baa596Sjoerg.\" Redistribution and use in source and binary forms, with or without
7c9baa596Sjoerg.\" modification, are permitted provided that the following conditions
8c9baa596Sjoerg.\" are met:
9c9baa596Sjoerg.\"
10c9baa596Sjoerg.\" 1. Redistributions of source code must retain the above copyright
11c9baa596Sjoerg.\"    notice, this list of conditions and the following disclaimer.
12c9baa596Sjoerg.\" 2. Redistributions in binary form must reproduce the above copyright
13c9baa596Sjoerg.\"    notice, this list of conditions and the following disclaimer in
14c9baa596Sjoerg.\"    the documentation and/or other materials provided with the
15c9baa596Sjoerg.\"    distribution.
16c9baa596Sjoerg.\"
17c9baa596Sjoerg.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18c9baa596Sjoerg.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19c9baa596Sjoerg.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20c9baa596Sjoerg.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
21c9baa596Sjoerg.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22c9baa596Sjoerg.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23c9baa596Sjoerg.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24c9baa596Sjoerg.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25c9baa596Sjoerg.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26c9baa596Sjoerg.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27c9baa596Sjoerg.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28c9baa596Sjoerg.\" SUCH DAMAGE.
29a2fb076eSriastradh.Dd August 31, 2022
30c9baa596Sjoerg.Dt REALLOCARR 3
31c9baa596Sjoerg.Os
32c9baa596Sjoerg.Sh NAME
33c9baa596Sjoerg.Nm reallocarr
34c9baa596Sjoerg.Nd reallocate array
35c9baa596Sjoerg.Sh SYNOPSIS
36c9baa596Sjoerg.In stdlib.h
37c9baa596Sjoerg.Ft int
38c9baa596Sjoerg.Fo reallocarr
39249e1d8cSriastradh.Fa "void *ptrp"
40638c990bSkamil.Fa "size_t number"
41c9baa596Sjoerg.Fa "size_t size"
42c9baa596Sjoerg.Fc
43c9baa596Sjoerg.Sh DESCRIPTION
44c9baa596SjoergThe
45c9baa596Sjoerg.Nm
46249e1d8cSriastradhfunction safely allocates, resizes, or frees arrays in memory.
47249e1d8cSriastradh.Pp
48249e1d8cSriastradhIf
49249e1d8cSriastradh.Fa ptr
50249e1d8cSriastradhis a null pointer, or a pointer to memory previously allocated with
51249e1d8cSriastradh.Nm ,
52249e1d8cSriastradhthen
53249e1d8cSriastradh.Fo reallocarr
54249e1d8cSriastradh.Li & Ns Fa ptr ,
55249e1d8cSriastradh.Fa number ,
56249e1d8cSriastradh.Fa size
57249e1d8cSriastradh.Fc
58249e1d8cSriastradhallocates or reallocates the memory that
59249e1d8cSriastradh.Fa ptr
60249e1d8cSriastradhpoints to, possibly moving it to a different location in memory, so
61249e1d8cSriastradhthat it has space for an array of
62249e1d8cSriastradh.Fa number
63249e1d8cSriastradhelements of
64249e1d8cSriastradh.Fa size
65249e1d8cSriastradhbytes apiece.
66249e1d8cSriastradh.Nm
67249e1d8cSriastradhupdates
68249e1d8cSriastradh.Fa ptr
69249e1d8cSriastradhin case it was moved on success, and leaves it unchanged on failure.
70249e1d8cSriastradh.Pp
71249e1d8cSriastradhIf
72249e1d8cSriastradh.Fa ptr
73249e1d8cSriastradhwas previously allocated, the objects stored at
74249e1d8cSriastradh.Fa ptr Ns Li "[0]" ,
75249e1d8cSriastradh.Fa ptr Ns Li "[1]" ,
76249e1d8cSriastradh\&...,
77249e1d8cSriastradhup to the shorter of its old
78249e1d8cSriastradh.Fa number
79249e1d8cSriastradhand its new
80249e1d8cSriastradh.Fa number ,
81249e1d8cSriastradhare copied into the new memory, like
82249e1d8cSriastradh.Xr realloc 3 .
83249e1d8cSriastradh.Pp
84249e1d8cSriastradh.Fa ptr
85249e1d8cSriastradhmay be null and
86249e1d8cSriastradh.Fa number
87249e1d8cSriastradhmay be zero.
88249e1d8cSriastradh.Fa size
89249e1d8cSriastradhmust be nonzero.
90249e1d8cSriastradh.Pp
91249e1d8cSriastradhThe memory allocated by
92249e1d8cSriastradh.Nm
93249e1d8cSriastradhmay be freed with
94249e1d8cSriastradh.Fo reallocarr
95249e1d8cSriastradh.Li & Ns Fa ptr ,
96249e1d8cSriastradh.Li 0 ,
97249e1d8cSriastradh.Fa size
98249e1d8cSriastradh.Fc ,
99249e1d8cSriastradhwhich will always succeed and unconditionally set
100249e1d8cSriastradh.Fa ptr
101249e1d8cSriastradhto null.
102249e1d8cSriastradh.Pp
103a2fb076eSriastradhLike
104a2fb076eSriastradh.Xr calloc 3 ,
105a2fb076eSriastradh.Nm
106a2fb076eSriastradhfails gracefully if the product of
107a2fb076eSriastradh.Fa number
108a2fb076eSriastradhand
109a2fb076eSriastradh.Fa size
110a2fb076eSriastradhwould overflow the representable size of memory.
111a2fb076eSriastradhUnlike
112a2fb076eSriastradh.Xr calloc 3 ,
113a2fb076eSriastradhnew memory allocated by
114a2fb076eSriastradh.Nm
115a2fb076eSriastradhis not zero-initialized.
116a2fb076eSriastradh.Pp
117249e1d8cSriastradhThe
118249e1d8cSriastradh.Nm
119249e1d8cSriastradhfunction may alter
120249e1d8cSriastradh.Va errno
121249e1d8cSriastradhas a side effect.
122249e1d8cSriastradh.Pp
123249e1d8cSriastradhNote that the argument
124249e1d8cSriastradh.Fa ptrp
125249e1d8cSriastradhis a pointer to a pointer to allocated memory, unlike
126*cf221fb5Sriastradh.Xr realloc 3
127249e1d8cSriastradhwhich takes a pointer to allocated memory.
128c9baa596Sjoerg.Sh RETURN VALUES
129c9baa596SjoergOn successful completion,
130249e1d8cSriastradh.Nm
131c9baa596Sjoergreturns 0 and updates
132249e1d8cSriastradh.Fa ptr .
133249e1d8cSriastradhOtherwise, an
134249e1d8cSriastradh.Xr errno 2
135249e1d8cSriastradhis returned, and
136249e1d8cSriastradh.Fa ptr
137249e1d8cSriastradhand the memory it points to are unmodified.
138f95cba75Swiz.Sh EXAMPLES
139f95cba75SwizThe following uses
140f95cba75Swiz.Fn reallocarr
141249e1d8cSriastradhto initialize an array of
142249e1d8cSriastradh.Dv INITSIZE
143249e1d8cSriastradhintegers, then
144249e1d8cSriastradhresizes it to
145249e1d8cSriastradh.Dv NEWSIZE
146249e1d8cSriastradhelements, and finally frees it:
147f95cba75Swiz.Bd -literal -offset indent
148f95cba75Swizint *data = NULL;
149249e1d8cSriastradhint error = 0;
150f95cba75Swiz
151249e1d8cSriastradh/* allocate */
152249e1d8cSriastradherror = reallocarr(&data, INITSIZE, sizeof(*data));
153249e1d8cSriastradhif (error)
154249e1d8cSriastradh	errc(1, error, "reallocarr failed");
155249e1d8cSriastradh\&...
156249e1d8cSriastradh/* resize */
157249e1d8cSriastradherror = reallocarr(&data, NEWSIZE, sizeof(*data));
158249e1d8cSriastradhif (error)
159249e1d8cSriastradh	errc(1, error, "reallocarr failed on resize");
160249e1d8cSriastradh\&...
161249e1d8cSriastradh/* free */
162249e1d8cSriastradh(void)reallocarr(&data, 0, sizeof(*data));
163249e1d8cSriastradhassert(data == NULL);
164f95cba75Swiz.Ed
165c9baa596Sjoerg.Sh SEE ALSO
166*cf221fb5Sriastradh.Xr calloc 3 ,
167*cf221fb5Sriastradh.Xr realloc 3 ,
168*cf221fb5Sriastradh.Xr reallocarray 3
169c9baa596Sjoerg.Sh HISTORY
170c9baa596Sjoerg.Nm
171c9baa596Sjoergfirst appeared in
172c9baa596Sjoerg.Nx 7.0 .
173a1da3f78Swiz.Ox
174a1da3f78Swizintroduced the
175c9baa596Sjoerg.Xr reallocarray 3
176c9baa596Sjoergfunction for the same purpose, but the interface makes it difficult
177c9baa596Sjoergto correctly handle zero-sized allocations.
178