[RFC][PATCH v2] Add reallocarray function.

Rich Felker dalias@libc.org
Tue May 20 04:35:00 GMT 2014


On Mon, May 19, 2014 at 03:02:52PM +0000, Joseph S. Myers wrote:
> On Sun, 18 May 2014, R?diger Sonderfeld wrote:
> 
> > diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
> > index 00329a2..b75c28f 100644
> > --- a/stdlib/stdlib.h
> > +++ b/stdlib/stdlib.h
> > @@ -479,6 +479,13 @@ extern void *calloc (size_t __nmemb, size_t __size)
> >     between objects pointed by the old and new pointers.  */
> >  extern void *realloc (void *__ptr, size_t __size)
> >       __THROW __attribute_warn_unused_result__;
> > +/* Re-allocate the previously allocated block in PTR, making the new
> > +   block large enough for NMEMB elements of SIZE bytes each.  */
> > +/* __attribute_malloc__ is not used, because if realloc returns
> > +   the same pointer that was passed to it, aliasing needs to be allowed
> > +   between objects pointed by the old and new pointers.  */
> > +extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
> > +     __THROW __attribute_warn_unused_result__;
> 
> How has this patch been tested?  This function declaration needs 
> conditioning on __USE_GNU, as no standard includes this function in 
> <stdlib.h>; I'd have expected you to get failures of the conform/ tests 
> for stdlib.h when you ran the testsuite.

Not really related to this patch, but I think the comment about
__attribute_malloc__ that was copied from realloc is wrong. The new
and old pointers cannot alias because, if realloc succeeds (and I
would expect reallocarray to behave the same) the _value_ of the old
pointer is indeterminate and accessing it (e.g. comparing it with new)
invokes undefined behavior.

Rich



More information about the Libc-alpha mailing list