effect of free()
Dolf Grunbauer
dolf at idca.tds.PHILIPS.nl
Thu Sep 7 19:20:05 AEST 1989
In article <248 at seti.inria.fr> jourdan at minos.inria.fr (Martin Jourdan) writes:
->In article <16022 at vail.ICO.ISC.COM> rcd at ico.ISC.COM (Dick Dunn) writes:
->>bill at 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.
->>Not true. The "if" only examines the value of the pointer, not what it
->>points to.
->You're wrong, Dick. Someone else already pointed it out, but let me
->make it clear again. Imagine a segmented memory architecture with
->protections, and imagine that the call to "free" above frees the last
->used block in the segment and that "free" is clever enough to
->determine it and decides to release the segment to the OS, thus making
->the whole segment invalid [...]
-> [...]. Then merely loading the value of "ptr" in a register to test it
->against 0 will cause a invalid-address trap.
I agree with Dick, as the original statement is:
if (ptr == 0) ...
I think Martin explained why the statement:
if (*ptr == 0) ...
is unacceptable and how the OS could trigger this illegal use.
Is this true or am I missing Martin's point ?
--
Dolf Grunbauer Tel: +31 55 432764 Internet dolf at idca.tds.philips.nl
Philips Telecommunication and Data Systems UUCP ....!mcvax!philapd!dolf
Dept. SSP, P.O. Box 245, 7300 AE Apeldoorn, The Netherlands
More information about the Comp.lang.c
mailing list