validity of free() on later pointer operations
Conor P. Cahill
cpcahil at virtech.uucp
Wed Jul 4 09:32:55 AEST 1990
In article <1990Jul3.185032.8434 at indetech.com> schmidt at indetech.UUCP (Doug Schmidt) writes:
> char *s = malloc (n);
> /* ... */
> free (s);
> s = 0; /* Is this assignment always legal? */
>
> I seem to recall there was a problem with the assignment to
>char *s *after* the free. Can someone please refresh my memory
>as to what the problem was, and why it is a problem? Would
>replacing s = 0 by s = malloc (n) have the same problem?
There is no problem with s = 0. However on some "hypothetical" systems,
the line:
if( s == 0 )
can cause the program to die, since you are causing an illegal pointer
to be loaded into an address register.
I don't know of any current machine where that will happen, but it
could be the case on some system in the future. What *almost* guarrantees
that it won't cause the program to die is the fact that on most, if not
all, implementations free() does not actually return space to the
OS.
If you want to be safe an absolute 100% of the time, don't use a freed
pointer (including dereferencing, or comparing). If 99.99999% saftey
is ok, then just be sure to not dereference it.
--
Conor P. Cahill (703)430-9247 Virtual Technologies, Inc.,
uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160
Sterling, VA 22170
More information about the Comp.lang.c
mailing list