1fc0219a1SBaptiste Daroussin.\" Copyright (c) 1980, 1991, 1993 2*3ef1b3b5SSergey 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.\" $FreeBSD$ 30450dfafbSBaptiste Daroussin.\" 31450dfafbSBaptiste Daroussin.Dd May 1, 2015 32450dfafbSBaptiste Daroussin.Dt REALLOCARRAY 3 33450dfafbSBaptiste Daroussin.Os 34450dfafbSBaptiste Daroussin.Sh NAME 35450dfafbSBaptiste Daroussin.Nm reallocarray 36450dfafbSBaptiste Daroussin.Nd memory reallocation function 37450dfafbSBaptiste Daroussin.Sh LIBRARY 38450dfafbSBaptiste Daroussin.Lb libc 39450dfafbSBaptiste Daroussin.Sh SYNOPSIS 40450dfafbSBaptiste Daroussin.In stdlib.h 41450dfafbSBaptiste Daroussin.Ft void * 42450dfafbSBaptiste Daroussin.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size" 43450dfafbSBaptiste Daroussin.Sh DESCRIPTION 44450dfafbSBaptiste DaroussinThe 45450dfafbSBaptiste Daroussin.Fn reallocarray 46*3ef1b3b5SSergey Kandaurovfunction is similar to the 47*3ef1b3b5SSergey Kandaurov.Fn realloc 48*3ef1b3b5SSergey Kandaurovfunction 49450dfafbSBaptiste Daroussinexcept it operates on 50450dfafbSBaptiste Daroussin.Fa nmemb 51450dfafbSBaptiste Daroussinmembers of size 52450dfafbSBaptiste Daroussin.Fa size 53450dfafbSBaptiste Daroussinand checks for integer overflow in the calculation 54450dfafbSBaptiste Daroussin.Fa nmemb 55450dfafbSBaptiste Daroussin* 56450dfafbSBaptiste Daroussin.Fa size . 57450dfafbSBaptiste Daroussin.Sh RETURN VALUES 58*3ef1b3b5SSergey KandaurovThe 59450dfafbSBaptiste Daroussin.Fn reallocarray 60*3ef1b3b5SSergey Kandaurovfunction returns a pointer to the allocated space; otherwise, a 61450dfafbSBaptiste Daroussin.Dv NULL 62450dfafbSBaptiste Daroussinpointer is returned and 63450dfafbSBaptiste Daroussin.Va errno 64450dfafbSBaptiste Daroussinis set to 65450dfafbSBaptiste Daroussin.Er ENOMEM . 66450dfafbSBaptiste Daroussin.Sh EXAMPLES 67450dfafbSBaptiste DaroussinConsider 68450dfafbSBaptiste Daroussin.Fn reallocarray 69450dfafbSBaptiste Daroussinwhen there is multiplication in the 70450dfafbSBaptiste Daroussin.Fa size 71450dfafbSBaptiste Daroussinargument of 72450dfafbSBaptiste Daroussin.Fn malloc 73450dfafbSBaptiste Daroussinor 74450dfafbSBaptiste Daroussin.Fn realloc . 75450dfafbSBaptiste DaroussinFor example, avoid this common idiom as it may lead to integer overflow: 76450dfafbSBaptiste Daroussin.Bd -literal -offset indent 77450dfafbSBaptiste Daroussinif ((p = malloc(num * size)) == NULL) 78450dfafbSBaptiste Daroussin err(1, "malloc"); 79450dfafbSBaptiste Daroussin.Ed 80450dfafbSBaptiste Daroussin.Pp 81450dfafbSBaptiste DaroussinA drop-in replacement is the 82450dfafbSBaptiste Daroussin.Ox 83450dfafbSBaptiste Daroussinextension 84450dfafbSBaptiste Daroussin.Fn reallocarray : 85450dfafbSBaptiste Daroussin.Bd -literal -offset indent 86450dfafbSBaptiste Daroussinif ((p = reallocarray(NULL, num, size)) == NULL) 87450dfafbSBaptiste Daroussin err(1, "reallocarray"); 88450dfafbSBaptiste Daroussin.Ed 89450dfafbSBaptiste Daroussin.Pp 90450dfafbSBaptiste DaroussinWhen using 91450dfafbSBaptiste Daroussin.Fn realloc , 92450dfafbSBaptiste Daroussinbe careful to avoid the following idiom: 93450dfafbSBaptiste Daroussin.Bd -literal -offset indent 94450dfafbSBaptiste Daroussinsize += 50; 95450dfafbSBaptiste Daroussinif ((p = realloc(p, size)) == NULL) 96450dfafbSBaptiste Daroussin return (NULL); 97450dfafbSBaptiste Daroussin.Ed 98450dfafbSBaptiste Daroussin.Pp 99450dfafbSBaptiste DaroussinDo not adjust the variable describing how much memory has been allocated 100450dfafbSBaptiste Daroussinuntil the allocation has been successful. 101450dfafbSBaptiste DaroussinThis can cause aberrant program behavior if the incorrect size value is used. 102450dfafbSBaptiste DaroussinIn most cases, the above sample will also result in a leak of memory. 103450dfafbSBaptiste DaroussinAs stated earlier, a return value of 104450dfafbSBaptiste Daroussin.Dv NULL 105450dfafbSBaptiste Daroussinindicates that the old object still remains allocated. 106450dfafbSBaptiste DaroussinBetter code looks like this: 107450dfafbSBaptiste Daroussin.Bd -literal -offset indent 108450dfafbSBaptiste Daroussinnewsize = size + 50; 109450dfafbSBaptiste Daroussinif ((newp = realloc(p, newsize)) == NULL) { 110450dfafbSBaptiste Daroussin free(p); 111450dfafbSBaptiste Daroussin p = NULL; 112450dfafbSBaptiste Daroussin size = 0; 113450dfafbSBaptiste Daroussin return (NULL); 114450dfafbSBaptiste Daroussin} 115450dfafbSBaptiste Daroussinp = newp; 116450dfafbSBaptiste Daroussinsize = newsize; 117450dfafbSBaptiste Daroussin.Ed 118450dfafbSBaptiste Daroussin.Pp 119450dfafbSBaptiste DaroussinAs with 120450dfafbSBaptiste Daroussin.Fn malloc , 121450dfafbSBaptiste Daroussinit is important to ensure the new size value will not overflow; 122450dfafbSBaptiste Daroussini.e. avoid allocations like the following: 123450dfafbSBaptiste Daroussin.Bd -literal -offset indent 124450dfafbSBaptiste Daroussinif ((newp = realloc(p, num * size)) == NULL) { 125450dfafbSBaptiste Daroussin ... 126450dfafbSBaptiste Daroussin.Ed 127450dfafbSBaptiste Daroussin.Pp 128450dfafbSBaptiste DaroussinInstead, use 129450dfafbSBaptiste Daroussin.Fn reallocarray : 130450dfafbSBaptiste Daroussin.Bd -literal -offset indent 131450dfafbSBaptiste Daroussinif ((newp = reallocarray(p, num, size)) == NULL) { 132450dfafbSBaptiste Daroussin ... 133450dfafbSBaptiste Daroussin.Ed 134450dfafbSBaptiste Daroussin.Sh SEE ALSO 135450dfafbSBaptiste Daroussin.Xr realloc 3 136450dfafbSBaptiste Daroussin.Sh HISTORY 137450dfafbSBaptiste DaroussinThe 138*3ef1b3b5SSergey Kandaurov.Fn reallocarray 139450dfafbSBaptiste Daroussinfunction first appeared in 140*3ef1b3b5SSergey Kandaurov.Ox 5.6 141*3ef1b3b5SSergey Kandaurovand 142*3ef1b3b5SSergey Kandaurov.Fx 11.0 . 143