calloc and realloc semantics

Christopher R Volpe volpe at camelback.crd.ge.com
Wed Feb 6 00:59:09 AEST 1991


In article <4865 at cui.unige.ch>, afzal at cui.unige.ch (Afzal Ballim) writes:
|>.
|>X *array1, array2;

             ^^^^^^^^  I think you mean *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? 
  
  The value of the variable "array1" is unchanged, but the storage it
points to is not necessarily valid.

|> Does it still point to the
|>originally sized array?  

  That array no longer exists.

|>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. 

  That's fine. All three routines (calloc, malloc, realloc) are guaranteed
to satisfy the strictest alignment requirement on the machine.

|>----------------------------------------------------------------------
---------
|>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
                                           
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com



More information about the Comp.lang.c mailing list