Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!julius.cs.uiuc.edu!ux1.cso.uiuc.edu!news From: mcdonald@aries.scs.uiuc.edu (Doug McDonald) Newsgroups: comp.lang.c Subject: Re: realloc() (was: Re: Safe coding practices) Message-ID: <1991Jan30.201955.21797@ux1.cso.uiuc.edu> Date: 30 Jan 91 20:19:55 GMT References: <1991Jan30.193308.3897@athena.mit.edu> <23975:Jan2516:36:5891@kramden.acf.nyu.edu> <1991Jan30.121425.16882@unhd.unh.edu> Sender: news@ux1.cso.uiuc.edu (News) Organization: University of Illinois at Urbana Lines: 28 In article <1991Jan30.193308.3897@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes: > > I believe that if the realloc() fails, the memory block pointed to by the >pointer passed into realloc() is no longer guaranteed to be valid. Therefore, >Jaeschke is right -- after realloc() returns NULL, you should not try to use >the block whose address you passed into realloc(). > " void *realloc(void *p, size_t size); realloc changes the size of the object pointed to by p to size. The contents will be unchanged up to the minimum of the old and new sizes. If the new size is larger the new space is uninitialized. Realloc returns a pointer to the new space, or NULL if the request cannot be satisfied, in which case *p is unchanged." The "" indicate a quote from K&R II. Note the last sentence: it says that *p is unchanged. *p is the CONTENTS of what p originally (and presumably still must) point to. So the quote preceeded by > signs is not true for a correct realloc. You CAN reuse p and *p after a failed realloc. Doug McDonald