effect of free()

Barry Margolin barmar at think.COM
Sun Sep 10 04:23:07 AEST 1989


In article <172 at cpsolv.UUCP> rhg at cpsolv.uucp (Richard H. Gumpertz) writes:
>The pointer comparison context is fundamentally
>different from the pointer indirection context; the former should be able to
>work with invalid pointer values (at least when the comparison is to NULL)
>while the latter may fault.

Why do you feel that comparing an invalid pointer to NULL is some sort
of special case?  If it were required not to fault, it would also
probably not be equal to NULL.  I imagine that code that begins with
"if (ptr != NULL)" is likely to go on to dereference the pointer (why
else would it bother checking the nullness?).  So what's the point of
making the comparison valid?

What may be needed to satisfy everyone is a built-in operation that
tells whether a pointer is valid to use.  It would have to be an
operator, not a function, so that it could get around the invalidity
of accessing freed pointers.  The definition of it would have to be
loose enough to permit efficient implementation on systems without
memory protection hardware (you don't want it running through malloc's
data structures to see whether it points into an allocated or free
block).


Barry Margolin
Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list