1REALLOCARRAY(3bsd)                   LOCAL                  REALLOCARRAY(3bsd)
2

NAME

4     reallocarray — memory allocation and deallocation
5

LIBRARY

7     Utility functions from BSD systems (libbsd, -lbsd)
8

SYNOPSIS

10     #include <stdlib.h>
11     (See libbsd(7) for include usage.)
12
13     void *
14     reallocarray(void *ptr, size_t nmemb, size_t size);
15

DESCRIPTION

17     When using malloc() be careful to avoid the following idiom:
18
19           if ((p = malloc(num * size)) == NULL)
20                   err(1, "malloc");
21
22     The multiplication may lead to an integer overflow, which can be avoided
23     using the extension reallocarray(), as follows:
24
25           if ((p = reallocarray(NULL, num, size)) == NULL)
26                   err(1, "malloc");
27
28     Alternatively calloc() is a more portable solution which comes with the
29     cost of clearing memory.
30
31     If malloc() must be used, be sure to test for overflow:
32
33           if (size && num > SIZE_MAX / size) {
34                   errno = ENOMEM;
35                   err(1, "overflow");
36           }
37
38     The use of reallocarray() or calloc() is strongly encouraged when allo‐
39     cating multiple sized objects in order to avoid possible integer over‐
40     flows.
41

RETURN VALUES

43     The reallocarray() function returns a pointer to the allocated space if
44     successful; otherwise, a null pointer is returned and errno is set to
45     ENOMEM.
46

SEE ALSO

48     malloc(3), calloc(3), alloca(3)
49

HISTORY

51     reallocarray() appeared in OpenBSD 5.6, glibc 2.26.
52
53BSD                              June 20, 2019                             BSD
Impressum