realloc() (was: Re: Safe coding practices)

Doug McDonald mcdonald at aries.scs.uiuc.edu
Thu Jan 31 07:19:55 AEST 1991


In article <1991Jan30.193308.3897 at athena.mit.edu> jik at 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



More information about the Comp.lang.c mailing list