Path: utzoo!attcan!uunet!cs.utexas.edu!csd4.csd.uwm.edu!mailrus!ncar!ico!vail!rcd
From: rcd@ico.ISC.COM (Dick Dunn)
Newsgroups: comp.lang.c
Subject: Re: effect of free()
Summary: no, this is safe
Message-ID: <16022@vail.ICO.ISC.COM>
Date: 17 Aug 89 22:57:27 GMT
References: <319@cubmol.BIO.COLUMBIA.EDU> <3756@buengc.BU.EDU> <1989Aug17.005548.745@twwells.com>
Organization: Interactive Systems Corp, Boulder, CO
Lines: 31

bill@twwells.com (T. William Wells) writes:
> ...For example, the following code fragment is nonportable:
> 
> 	char    *ptr;
> 
> 	ptr = malloc(1);
> 	...
> 	free(ptr);
> 	...
> 	if (ptr == 0) ...
> 
> The if might cause a trap when the value of ptr is accessed.

Not true.  The "if" only examines the value of the pointer, not what it
points to.  free(ptr) does not modify ptr; it can't.  Assuming the malloc
succeeded (and assuming an appropriate in-scope declaration of malloc, if
you want to be fussy), the code as written will work, and the body of the
"if" will not be executed, since ptr will be non-null from the malloc.

The trouble will only begin when you try to look at *ptr.

shut up,
inews!
I
know
what
I'm
doing.
-- 
Dick Dunn     rcd@ico.isc.com    uucp: {ncar,nbires}!ico!rcd     (303)449-2870
   ...Are you making this up as you go along?