effect of free()

Richard O'Keefe ok at cs.mu.oz.au
Sat Sep 16 16:31:45 AEST 1989


In article <33383 at ism780c.isc.com>, news at ism780c.isc.com (News system) writes:
> I don't understand this discussion.  Inspecting a pointer variable that is
> not initialzed is no different from inspecting ANY uninitialized variable.  A
> pointer that has free applied to it is unitialized.  How in the world could
> any programming language be defined to assign a useful meaning to the
> inspection uninitialized data?  Hint: defining the state of local data at
> procedure entry can be very expensive.
> 
>     Marv Rubinstein

The context of the discussion is
	p = NULL;
	...
	if (hairy-test-expression) {
	    ...
	    p = malloc(some_size);
	    assert(p != NULL);
	    ...
	    free(p);
	    ...
	}
	if (p != NULL) ...   <- may go BANG! here.

Look at it carefully:  p is NOT "uninitialised".  Whether program execution
took the "p = malloc()" path or just fell straight through, p has in fact
been initialised to a meaningful value.  free() does not have any access to
p, only to the value of p.  There may be fifteen million variables all with
the same value as p; free() has no access to them either.  Hint: locating
all the variables with the same value as p and making them uninitialised
can be very expensive.

It appears that the draft standard DOES allow the program to do anything
the implementor pleases at the marked point, and that that was a careful
and deliberate choice based on the belief that there might be machines where
that might make sense (though not 680x0s, VAXen, /370s, 80*86s, or PR1MEs).
However, this has nothing to do with uninitialised variables.

The longer I use C, the better I like Ada, and I started out _hating_ Ada.



More information about the Comp.lang.c mailing list