Failure INSIDE malloc() ??

Daniel Jimenez djimenez at ringer.cs.utsa.edu
Thu Jun 27 15:44:57 AEST 1991


In article <phil.7974 at phd.UUCP> phil at phd.UUCP (H Phil Duby) writes:
>In article <677448705 at macbeth.cs.duke.edu> lsn at duke.cs.duke.edu (Lars S. Nyland) writes:
>> I would bet that you still use a pointer after you have freed
>> the memory it points to.  You might amend your free statements
>> to be more like:
>>       free(p); p = NULL;
>> just to make sure that you don't use the pointers after you have
>> freed them.
>Setting a pointer to NULL does not (at least on some systems) prevent it
>from being used.  On my AMIGA, NULL <IS> zero, and is a valid memory
>location. There are <run time> tools that detect (and repair) accesses
>(especially modifications) to the low memory locations, but this does not
>prevent a program from <attempting> to use that memory.  The offending

I would add that free (p); p = NULL; doesn't do the trick, even if
NULL is a completely invalid address, like all bits one.  There could
still be *other* pointers hanging around that are aliased to what p
used to point to.  This kind of thing has caused me endless headaches.

q = p;
...
free (p); p = NULL;
...
q->info = 'A'; /* Argh!  It'll take me weeks to find this bug! */
-- 
*    Daniel A. Jimenez			*  Please excuse my longwindedness.
*    djimenez at ringer.cs.utsa.edu	*  This Sun terminal makes everything
*    dajim at lonestar.utsa.edu		*  I write seem important.
* Opinions expressed here are mine only, and not those of UTSA.



More information about the Comp.lang.c mailing list