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