REALLOCARRAY(3)

πŸ“¦ version 15.1, in freebsd 15.1

πŸ’– NAME πŸ’–

reallocarray β€” memory reallocation function.

Ok bestie, quick vibe check: this is REALLOCARRAY(3) from the Library Functions Manual, and it’s literally the memory reallocation function page. πŸ’–


πŸŽ€ LIBRARY πŸŽ€

Standard C Library (libc, -lc).


✨ SYNOPSIS ✨

#include <stdlib.h>

void *
reallocarray(void *ptr, size_t nmemb, size_t size);

void *
recallocarray(void *ptr, size_t oldnmeb, size_t nmemb, size_t, size");

🌸 DESCRIPTION 🌸

Okay so reallocarray() is basically realloc()’s cousin, except it operates on nmemb members of size size and checks for integer overflow in the calculation nmemb * size. That overflow check is the whole point, bestie β€” it’s giving safety rails πŸ›€οΈ.

recallocarray() is similar to reallocarray() except it ensures newly allocated memory is cleared similar to calloc(). If ptr is NULL, then oldnmemb is ignored and the call is equivalent to calloc(). If ptr is not NULL, oldnmemb must be a value such that oldnmemb * size is the size of the earlier allocation that returned ptr β€” otherwise the behaviour is undefined. Periodt. 🚨


πŸ’… RETURN VALUES πŸ’…

reallocarray() returns a pointer to the allocated space; otherwise, a NULL pointer is returned and errno is set to ENOMEM. That’s the return-value tea, no cap. ✨


πŸ¦‹ EXAMPLES πŸ¦‹

Consider reallocarray() when there is multiplication in the size argument of malloc() or realloc(). For example, avoid this common idiom as it may lead to integer overflow:

       if ((p = malloc(num * size)) == NULL)
               err(1, "malloc");

A drop-in replacement is the OpenBSD extension reallocarray():

       if ((p = reallocarray(NULL, num, size)) == NULL)
               err(1, "reallocarray");

When using realloc(), be careful to avoid the following idiom:

       size += 50;
       if ((p = realloc(p, size)) == NULL)
               return (NULL);

Do not adjust the variable describing how much memory has been allocated until the allocation has been successful. This can cause aberrant program behavior if the incorrect size value is used. In most cases, the above sample will also result in a leak of memory. As stated earlier, a return value of NULL indicates that the old object still remains allocated. Better code looks like this:

       newsize = size + 50;
       if ((newp = realloc(p, newsize)) == NULL) {
               free(p);
               p = NULL;
               size = 0;
               return (NULL);
       }
       p = newp;
       size = newsize;

As with malloc(), it is important to ensure the new size value will not overflow; i.e. avoid allocations like the following:

       if ((newp = realloc(p, num * size)) == NULL) {
               ...

Instead, use reallocarray():

       if ((newp = reallocarray(p, num, size)) == NULL) {
               ...

πŸ‘€ SEE ALSO πŸ‘€


🌈 STANDARDS 🌈

reallocarray() conforms to .

Yes bestie, the blank after β€œto” is exactly what the page says β€” no cap. 😭


πŸ«– HISTORY πŸ«–

The reallocarray() function first appeared in OpenBSD 5.6 and FreeBSD 11.0. The recallocarray() function first appeared in OpenBSD 6.1 and FreeBSD 15.1.


xoxo, your Debian bestie πŸ’… β€” October 2, 2025, page REALLOCARRAY(3). Source/version: Debian. ✨

πŸ“¦ where this page comes from
  • freebsd 15.1: base 15.1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms