Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!cernvax!chx400!ugun2b!cui!afzal From: afzal@cui.unige.ch (Afzal Ballim) Newsgroups: comp.lang.c Subject: calloc and realloc semantics Keywords: calloc realloc Message-ID: <4865@cui.unige.ch> Date: 4 Feb 91 12:01:47 GMT Organization: University of Geneva, Switzerland Lines: 43 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