calloc and realloc semantics

Afzal Ballim afzal at cui.unige.ch
Mon Feb 4 23:01:47 AEST 1991


Hi, I am somewhat confused by the documentation I have found on C memory
allocation routines, and was wondering if someone could help me. In particular,
I am concerned with calloc and realloc.

Is it safe (and portable) to use realloc to resize an array that
was generated using calloc?

So, for example, for some arbitrary type X, if we do the following...


.
.
.
X *array1, array2;
.
.
/* n is the size we begin with */
array1 = (X *) calloc(n,sizeof(X));
.
.
.
/* m is the new size that we want */
array2 = (X *) realloc(array1,m*sizeof(X));
.
.
.


After the realloc, what is the status of array1?  Does it still point to the
originally sized array?  Even if the block has been "moved" as the
documentation (of Sun's C) says may happen?

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

Any help would be appreciated.

------------------------------------------------------------------------------
Afzal Ballim	             |EAN,BITNET,EARN,MHS,X.400: afzal at divsun.unige.ch
 ISSCO, University of Geneva |UUCP: mcvax!cernvax!cui!divsun.unige.ch!afzal
 54 route des Acacias	     |JANET: afzal%divsun.unige.ch at uk.ac.ean-relay
 CH-1227 GENEVA,Switzerland  |CSNET,ARPA: afzal%divsun.unige.ch at relay.cs.net



More information about the Comp.lang.c mailing list