effect of free()

Henry Spencer henry at utzoo.uucp
Sat Aug 19 04:50:11 AEST 1989


In article <16022 at vail.ICO.ISC.COM> rcd at ico.ISC.COM (Dick Dunn) writes:
>> 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 ... the code as written will work...
>The trouble will only begin when you try to look at *ptr.

Dick, can you cite chapter and verse in X3J11 saying that it is legal to
examine a pointer which points to freed storage?  If not, I would say that
you are making an implementation-dependent assumption.  On some machines,
pointers are very special animals, held in special registers that "know"
that their contents are pointers, and loading a random value into such a
register can cause trouble even if you don't dereference it.  Whether any
of them care about whether the pointer points to valid storage is another
question.  But it's not clear to me that such behavior is ruled out.
-- 
V7 /bin/mail source: 554 lines.|     Henry Spencer at U of Toronto Zoology
1989 X.400 specs: 2200+ pages. | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list