reallocarr
—
reallocate array
#include <stdlib.h>
int
reallocarr
(void *ptr,
size_t number, size_t size);
The reallocarr
function reallocates the memory in
*ptr.
On successful completion, returns 0 and updates *ptr.
Otherwise, an error code (see
errno(2)) is returned and
*ptr and the referenced memory is unmodified.
The following uses reallocarr
() to initialize an array
of INITSIZE integers, then resizes it to NEWSIZE elements:
int *data = NULL;
int ret = 0;
ret = reallocarr(&data, INITSIZE, sizeof(*data));
if (ret)
errc(1, ret, "reallocarr failed");
ret = reallocarr(&data, NEWSIZE, sizeof(*data));
if (ret)
errc(1, ret, "reallocarr failed on resize");
reallocarr
first appeared in NetBSD
7.0. OpenBSD introduced the
reallocarray(3) function
for the same purpose, but the interface makes it difficult to correctly handle
zero-sized allocations.