SIMPLE SOLUTIONS

REALLOCARRAY(3BSD) - Linux man page online | Library functions

Memory allocation and deallocation.

Chapter
May 1, 2014
REALLOCARRAY(3bsd) LOCAL REALLOCARRAY(3bsd)

NAME

reallocarray — memory allocation and deallocation

LIBRARY

Utility functions from BSD systems (libbsd, -lbsd)

SYNOPSIS

#include <stdlib.h> (See libbsd(7) for include usage.) void * reallocarray(void *ptr, size_t nmemb, size_t size);

DESCRIPTION

When using malloc() be careful to avoid the following idiom: if ((p = malloc(num * size)) == NULL) err(1, "malloc"); The multiplication may lead to an integer overflow, which can be avoided using the extension reallocarray(), as follows: if ((p = reallocarray(NULL, num, size)) == NULL) err(1, "malloc"); Alternatively calloc() is a more portable solution which comes with the cost of clearing memory. If malloc() must be used, be sure to test for overflow: if (size && num > SIZE_MAX / size) { errno = ENOMEM; err(1, "overflow"); } The use of reallocarray() or calloc() is strongly encouraged when allocating multiple sized objects in order to avoid possible integer overflows.

RETURN VALUES

The reallocarray() function returns a pointer to the allocated space if successful; other‐ wise, a null pointer is returned and errno is set to ENOMEM.

SEE ALSO

malloc(3), calloc(3), alloca(3)

HISTORY

reallocarray() appeared in OpenBSD 5.6, glibc 2.26.
BSD May 1, 2014 BSD
This manual Reference Other manuals
reallocarray(3bsd) referred by libbsd(7)
refer to alloca(3) | libbsd(7) | malloc(3)
Download raw manual
Main page LOCAL (+267) BSD (+3984) № 3 (+68044)
Go top