effect of free()

DAVID NEWALL CCDN at levels.sait.edu.au
Fri Sep 8 23:33:17 AEST 1989


jourdan at seti.inria.fr (jourdan martin joseph) 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:
>>>     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.
>
> merely loading the value of "ptr" in a register to test it against 0 will
> cause a invalid-address trap.

Why?  Loading the value of a pointer is not the same as dereferencing that
pointer?  I can't see why you should get an invalid-address trap.  In fact,
a machine that *did* trap in such a case, would be decidedly unfriendly.


David Newall                     Phone:  +61 8 343 3160
Unix Systems Programmer          Fax:    +61 8 349 6939
Academic Computing Service       E-mail: ccdn at levels.sait.oz.au
SA Institute of Technology       Post:   The Levels, South Australia, 5095



More information about the Comp.lang.c mailing list