[comp.lang.c] calloc and realloc semantics

afzal@cui.unige.ch (Afzal Ballim) (02/04/91)

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@divsun.unige.ch
 ISSCO, University of Geneva |UUCP: mcvax!cernvax!cui!divsun.unige.ch!afzal
 54 route des Acacias	     |JANET: afzal%divsun.unige.ch@uk.ac.ean-relay
 CH-1227 GENEVA,Switzerland  |CSNET,ARPA: afzal%divsun.unige.ch@relay.cs.net

volpe@camelback.crd.ge.com (Christopher R Volpe) (02/05/91)

In article <4865@cui.unige.ch>, afzal@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@divsun.unige.ch
|> ISSCO, University of Geneva |UUCP: mcvax!cernvax!cui!divsun.unige.ch!afzal
|> 54 route des Acacias	     |JANET: afzal%divsun.unige.ch@uk.ac.ean-relay
|> CH-1227 GENEVA,Switzerland  |CSNET,ARPA: afzal%divsun.unige.ch@relay.cs.net
                                           
==================
Chris Volpe
G.E. Corporate R&D
volpecr@crd.ge.com

gwyn@smoke.brl.mil (Doug Gwyn) (02/06/91)

In article <4865@cui.unige.ch> afzal@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.