1fc0219a1SBaptiste Daroussin.\" Copyright (c) 1980, 1991, 1993 23ef1b3b5SSergey Kandaurov.\" The Regents of the University of California. All rights reserved. 3450dfafbSBaptiste Daroussin.\" 4450dfafbSBaptiste Daroussin.\" This code is derived from software contributed to Berkeley by 5450dfafbSBaptiste Daroussin.\" the American National Standards Committee X3, on Information 6450dfafbSBaptiste Daroussin.\" Processing Systems. 7450dfafbSBaptiste Daroussin.\" 8450dfafbSBaptiste Daroussin.\" Redistribution and use in source and binary forms, with or without 9450dfafbSBaptiste Daroussin.\" modification, are permitted provided that the following conditions 10450dfafbSBaptiste Daroussin.\" are met: 11450dfafbSBaptiste Daroussin.\" 1. Redistributions of source code must retain the above copyright 12450dfafbSBaptiste Daroussin.\" notice, this list of conditions and the following disclaimer. 13450dfafbSBaptiste Daroussin.\" 2. Redistributions in binary form must reproduce the above copyright 14450dfafbSBaptiste Daroussin.\" notice, this list of conditions and the following disclaimer in the 15450dfafbSBaptiste Daroussin.\" documentation and/or other materials provided with the distribution. 16450dfafbSBaptiste Daroussin.\" 17450dfafbSBaptiste Daroussin.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18450dfafbSBaptiste Daroussin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19450dfafbSBaptiste Daroussin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20450dfafbSBaptiste Daroussin.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21450dfafbSBaptiste Daroussin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22450dfafbSBaptiste Daroussin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23450dfafbSBaptiste Daroussin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24450dfafbSBaptiste Daroussin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25450dfafbSBaptiste Daroussin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26450dfafbSBaptiste Daroussin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27450dfafbSBaptiste Daroussin.\" SUCH DAMAGE. 28450dfafbSBaptiste Daroussin.\" 29450dfafbSBaptiste Daroussin.Dd May 1, 2015 30450dfafbSBaptiste Daroussin.Dt REALLOCARRAY 3 31450dfafbSBaptiste Daroussin.Os 32450dfafbSBaptiste Daroussin.Sh NAME 33450dfafbSBaptiste Daroussin.Nm reallocarray 34450dfafbSBaptiste Daroussin.Nd memory reallocation function 35450dfafbSBaptiste Daroussin.Sh LIBRARY 36450dfafbSBaptiste Daroussin.Lb libc 37450dfafbSBaptiste Daroussin.Sh SYNOPSIS 38450dfafbSBaptiste Daroussin.In stdlib.h 39450dfafbSBaptiste Daroussin.Ft void * 40450dfafbSBaptiste Daroussin.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size" 41450dfafbSBaptiste Daroussin.Sh DESCRIPTION 42450dfafbSBaptiste DaroussinThe 43450dfafbSBaptiste Daroussin.Fn reallocarray 443ef1b3b5SSergey Kandaurovfunction is similar to the 453ef1b3b5SSergey Kandaurov.Fn realloc 463ef1b3b5SSergey Kandaurovfunction 47450dfafbSBaptiste Daroussinexcept it operates on 48450dfafbSBaptiste Daroussin.Fa nmemb 49450dfafbSBaptiste Daroussinmembers of size 50450dfafbSBaptiste Daroussin.Fa size 51450dfafbSBaptiste Daroussinand checks for integer overflow in the calculation 52450dfafbSBaptiste Daroussin.Fa nmemb 53450dfafbSBaptiste Daroussin* 54450dfafbSBaptiste Daroussin.Fa size . 55450dfafbSBaptiste Daroussin.Sh RETURN VALUES 563ef1b3b5SSergey KandaurovThe 57450dfafbSBaptiste Daroussin.Fn reallocarray 583ef1b3b5SSergey Kandaurovfunction returns a pointer to the allocated space; otherwise, a 59450dfafbSBaptiste Daroussin.Dv NULL 60450dfafbSBaptiste Daroussinpointer is returned and 61450dfafbSBaptiste Daroussin.Va errno 62450dfafbSBaptiste Daroussinis set to 63450dfafbSBaptiste Daroussin.Er ENOMEM . 64450dfafbSBaptiste Daroussin.Sh EXAMPLES 65450dfafbSBaptiste DaroussinConsider 66450dfafbSBaptiste Daroussin.Fn reallocarray 67450dfafbSBaptiste Daroussinwhen there is multiplication in the 68450dfafbSBaptiste Daroussin.Fa size 69450dfafbSBaptiste Daroussinargument of 70450dfafbSBaptiste Daroussin.Fn malloc 71450dfafbSBaptiste Daroussinor 72450dfafbSBaptiste Daroussin.Fn realloc . 73450dfafbSBaptiste DaroussinFor example, avoid this common idiom as it may lead to integer overflow: 74450dfafbSBaptiste Daroussin.Bd -literal -offset indent 75450dfafbSBaptiste Daroussinif ((p = malloc(num * size)) == NULL) 76450dfafbSBaptiste Daroussin err(1, "malloc"); 77450dfafbSBaptiste Daroussin.Ed 78450dfafbSBaptiste Daroussin.Pp 79450dfafbSBaptiste DaroussinA drop-in replacement is the 80450dfafbSBaptiste Daroussin.Ox 81450dfafbSBaptiste Daroussinextension 82450dfafbSBaptiste Daroussin.Fn reallocarray : 83450dfafbSBaptiste Daroussin.Bd -literal -offset indent 84450dfafbSBaptiste Daroussinif ((p = reallocarray(NULL, num, size)) == NULL) 85450dfafbSBaptiste Daroussin err(1, "reallocarray"); 86450dfafbSBaptiste Daroussin.Ed 87450dfafbSBaptiste Daroussin.Pp 88450dfafbSBaptiste DaroussinWhen using 89450dfafbSBaptiste Daroussin.Fn realloc , 90450dfafbSBaptiste Daroussinbe careful to avoid the following idiom: 91450dfafbSBaptiste Daroussin.Bd -literal -offset indent 92450dfafbSBaptiste Daroussinsize += 50; 93450dfafbSBaptiste Daroussinif ((p = realloc(p, size)) == NULL) 94450dfafbSBaptiste Daroussin return (NULL); 95450dfafbSBaptiste Daroussin.Ed 96450dfafbSBaptiste Daroussin.Pp 97450dfafbSBaptiste DaroussinDo not adjust the variable describing how much memory has been allocated 98450dfafbSBaptiste Daroussinuntil the allocation has been successful. 99450dfafbSBaptiste DaroussinThis can cause aberrant program behavior if the incorrect size value is used. 100450dfafbSBaptiste DaroussinIn most cases, the above sample will also result in a leak of memory. 101450dfafbSBaptiste DaroussinAs stated earlier, a return value of 102450dfafbSBaptiste Daroussin.Dv NULL 103450dfafbSBaptiste Daroussinindicates that the old object still remains allocated. 104450dfafbSBaptiste DaroussinBetter code looks like this: 105450dfafbSBaptiste Daroussin.Bd -literal -offset indent 106450dfafbSBaptiste Daroussinnewsize = size + 50; 107450dfafbSBaptiste Daroussinif ((newp = realloc(p, newsize)) == NULL) { 108450dfafbSBaptiste Daroussin free(p); 109450dfafbSBaptiste Daroussin p = NULL; 110450dfafbSBaptiste Daroussin size = 0; 111450dfafbSBaptiste Daroussin return (NULL); 112450dfafbSBaptiste Daroussin} 113450dfafbSBaptiste Daroussinp = newp; 114450dfafbSBaptiste Daroussinsize = newsize; 115450dfafbSBaptiste Daroussin.Ed 116450dfafbSBaptiste Daroussin.Pp 117450dfafbSBaptiste DaroussinAs with 118450dfafbSBaptiste Daroussin.Fn malloc , 119450dfafbSBaptiste Daroussinit is important to ensure the new size value will not overflow; 120450dfafbSBaptiste Daroussini.e. avoid allocations like the following: 121450dfafbSBaptiste Daroussin.Bd -literal -offset indent 122450dfafbSBaptiste Daroussinif ((newp = realloc(p, num * size)) == NULL) { 123450dfafbSBaptiste Daroussin ... 124450dfafbSBaptiste Daroussin.Ed 125450dfafbSBaptiste Daroussin.Pp 126450dfafbSBaptiste DaroussinInstead, use 127450dfafbSBaptiste Daroussin.Fn reallocarray : 128450dfafbSBaptiste Daroussin.Bd -literal -offset indent 129450dfafbSBaptiste Daroussinif ((newp = reallocarray(p, num, size)) == NULL) { 130450dfafbSBaptiste Daroussin ... 131450dfafbSBaptiste Daroussin.Ed 132450dfafbSBaptiste Daroussin.Sh SEE ALSO 133450dfafbSBaptiste Daroussin.Xr realloc 3 134*dfa0ac74SEd Maste.Sh STANDARDS 135*dfa0ac74SEd Maste.Fn reallocarray 136*dfa0ac74SEd Masteconforms to 137*dfa0ac74SEd Maste.St -p1003.1-2024 . 138450dfafbSBaptiste Daroussin.Sh HISTORY 139450dfafbSBaptiste DaroussinThe 1403ef1b3b5SSergey Kandaurov.Fn reallocarray 141450dfafbSBaptiste Daroussinfunction first appeared in 1423ef1b3b5SSergey Kandaurov.Ox 5.6 1433ef1b3b5SSergey Kandaurovand 1443ef1b3b5SSergey Kandaurov.Fx 11.0 . 145