calloc and realloc semantics

Doug Gwyn gwyn at smoke.brl.mil
Wed Feb 6 06:50:57 AEST 1991


In article <4865 at cui.unige.ch> afzal at cui.unige.ch (Afzal Ballim) writes:
>Is it safe (and portable) to use realloc to resize an array that
>was generated using calloc?

calloc() is supposed to be simply an interface to malloc() that also
fills the allocated storage with 0-valued bytes.  Thus it isn't very
useful, but it should be safe to use in conjunction with other functions
in the malloc() family.

>... After the realloc, what is the status of array1?

If array2 is a null pointer, then array1 should still points to the
same data (and same-sized allocation) as before; otherwise, array1 is
now an invalid pointer and should not be further used.  In the latter
case, you must use array2 to access the (possibly moved) data.

>Is this use of newsize*sizeof(element) valid?  I am worried here about
>alignment of elements. 

Yes, there is no problem with that.



More information about the Comp.lang.c mailing list