Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!cs.utexas.edu!uunet!pilchuck!dataio!bright From: bright@Data-IO.COM (Walter Bright) Newsgroups: comp.lang.c Subject: Re: effect of free() Message-ID: <2101@dataio.Data-IO.COM> Date: 16 Aug 89 17:56:57 GMT References: <319@cubmol.BIO.COLUMBIA.EDU> <3756@buengc.BU.EDU><320@cubmol.BIO.COLUMBIA.EDU> Reply-To: bright@dataio.Data-IO.COM (Walter Bright) Organization: Data I/O Corporation; Redmond, WA Lines: 23 In article <320@cubmol.BIO.COLUMBIA.EDU> ping@cubmol.UUCP (Shiping Zhang) writes: >Many people said that after free() >is called, the pointer used as the argument to free() is still valid >and can be used IF NO (mc)alloc()'s are called after the call to free(). This is NOT true of all implementations of free(). Under Zortech C, the size of a freelist entry (6 bytes) is larger than the size (2 bytes) of the header of an allocated block. So when you free a block, the first 4 bytes of it get trashed. A pox on anyone who writes code like: for (p = listhead; p; p = p->next) free(p); Write it like: for (p = listhead; p; p = pn) { pn = p->next; free(p); } Program defensively. Always assume: 1. Calls to free() invalidate any pointers into that memory block. 2. realloc() always shifts the location of the block.