Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!uunet!kddlab!titcca!sragwa!wsgw!socslgw!diamond From: diamond@csl.sony.co.jp (Norman Diamond) Newsgroups: comp.lang.c Subject: Re: effect of free() Message-ID: <10817@riks.csl.sony.co.jp> Date: 8 Sep 89 05:42:06 GMT References: <319@cubmol.BIO.COLUMBIA.EDU> <3756@buengc.BU.EDU> <1989Aug17.005548.745@twwells.com> <16022@vail.ICO.ISC.COM> <248@seti.inria.fr> Reply-To: diamond@riks. (Norman Diamond) Organization: Sony Computer Science Laboratory Inc., Tokyo, Japan Lines: 45 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. In article <16022@vail.ICO.ISC.COM> rcd@ico.ISC.COM (Dick Dunn) writes: >>Not true. The "if" only examines the value of the pointer, not what it >>points to. In article <248@seti.inria.fr> jourdan@minos.inria.fr (Martin Jourdan) writes: >You're wrong, Dick.... Imagine a segmented memory architecture with >protections, and imagine that the call to "free" above frees the last >used block in the segment ... >The morale of the whole story is: DO NOT DO ANYTHING WITH A FREED >POINTER.... In fact the block cannot be freed by "free." It can be freed by the next malloc/calloc, and might be freed by the next realloc. However, if there are no *alloc calls between the free and the if, it has to work. Why? Well, because a freed pointer is still valid in a call to realloc (as long as there are no intervening *alloc calls), and the data still have to be sitting in limbo, not really quite freed yet. (Much to the chagrin of efficiency experts everywhere.) To cover all cases though: IT IS MADNESS TO DO ANYTHING WITH A FREED POINTER. -- -- Norman Diamond, Sony Corporation (diamond@ws.sony.junet) The above opinions are inherited by your machine's init process (pid 1), after being disowned and orphaned. However, if you see this at Waterloo or Anterior, then their administrators must have approved of these opinions.