1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the American National Standards Committee X3, on Information 6.\" Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.\" $FreeBSD: head/lib/libc/stdlib/reallocarray.3 282472 2015-05-05 10:44:17Z pluknet $ 30.\" 31.Dd April 11, 2019 32.Dt REALLOCARRAY 3 33.Os 34.Sh NAME 35.Nm reallocarray , 36.Nm recallocarray 37.Nd memory reallocation functions 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In stdlib.h 42.Ft void * 43.Fn reallocarray "void *ptr" "size_t number" "size_t size" 44.Ft void * 45.Fn recallocarray "void *ptr" "size_t oldnmemb" "size_t nmemb" "size_t size" 46.Sh DESCRIPTION 47The 48.Fn reallocarray 49function is similar to the 50.Fn realloc 51function except it operates on 52.Fa number 53members of size 54.Fa size 55and checks for integer overflow in the calculation 56.Fa number 57* 58.Fa size . 59.Pp 60The 61.Fn recallocarray 62function is similar to 63.Fn reallocarray 64except it ensures newly allocated memory is cleared similar to 65.Fn calloc . 66.Sh RETURN VALUES 67The 68.Fn reallocarray 69function returns a pointer to the allocated space; otherwise, a 70.Dv NULL 71pointer is returned and 72.Va errno 73is set to 74.Er ENOMEM . 75.Sh EXAMPLES 76Consider 77.Fn reallocarray 78when there is multiplication in the 79.Fa size 80argument of 81.Fn malloc 82or 83.Fn realloc . 84For example, avoid this common idiom as it may lead to integer overflow: 85.Bd -literal -offset indent 86if ((p = malloc(num * size)) == NULL) 87 err(1, "malloc"); 88.Ed 89.Pp 90A drop-in replacement is the 91.Ox 92extension 93.Fn reallocarray : 94.Bd -literal -offset indent 95if ((p = reallocarray(NULL, num, size)) == NULL) 96 err(1, "reallocarray"); 97.Ed 98.Pp 99When using 100.Fn realloc , 101be careful to avoid the following idiom: 102.Bd -literal -offset indent 103size += 50; 104if ((p = realloc(p, size)) == NULL) 105 return (NULL); 106.Ed 107.Pp 108Do not adjust the variable describing how much memory has been allocated 109until the allocation has been successful. 110This can cause aberrant program behavior if the incorrect size value is used. 111In most cases, the above sample will also result in a leak of memory. 112As stated earlier, a return value of 113.Dv NULL 114indicates that the old object still remains allocated. 115Better code looks like this: 116.Bd -literal -offset indent 117newsize = size + 50; 118if ((newp = realloc(p, newsize)) == NULL) { 119 free(p); 120 p = NULL; 121 size = 0; 122 return (NULL); 123} 124p = newp; 125size = newsize; 126.Ed 127.Pp 128As with 129.Fn malloc , 130it is important to ensure the new size value will not overflow; 131i.e. avoid allocations like the following: 132.Bd -literal -offset indent 133if ((newp = realloc(p, num * size)) == NULL) { 134 ... 135.Ed 136.Pp 137Instead, use 138.Fn reallocarray : 139.Bd -literal -offset indent 140if ((newp = reallocarray(p, num, size)) == NULL) { 141 ... 142.Ed 143.Sh SEE ALSO 144.Xr realloc 3 145.Sh HISTORY 146The 147.Fn reallocarray 148function first appeared in 149.Ox 5.6 150and 151.Dx 5.5 . 152.Pp 153The 154.Fn recallocarray 155function appeared in 156.Ox 6.1 157and 158.Dx 5.5 . 159