Life after free?

Chris Torek chris at mimsy.umd.edu
Sat Sep 29 16:58:00 AEST 1990


>In article <quan.654410256 at sol> quan at sol.surv.utas.oz (Stephen Quan) writes:
>>Any comments on free-ing tmp before it is return-ed?

In article <5620 at stpstn.UUCP> lerman at stpstn.UUCP (Ken Lerman) writes:
>Not only is it sensible, the semantics of free require it.

Nay, not so.

>So, yeah, it could be used.  Is it useful (in the sense of full of
>use)? No way.

Aye.

To sum up: in many implementations, the space released via free()
remains unchanged at least until a later call to malloc or calloc.
A few even document this behaviour.  It is, however, not guaranteed
by the closest thing we have to a standard (i.e., ANSI X3.159-1989,
a.k.a. `ANSI C'), and there are some implementations out there that
will in fact immediately stomp on the space released by free().

As to the other question---that of returning the value of an automatic
---I think it has been sufficiently answered, but just for the record,
there is nothing wrong with returning the value of an automatic `char *'
variable:

	char *f() {
		char *p;
		int g();
		p = g() ? "yes" : "no";
		return p;
	}

This is no worse than, e.g.,

	int f1() {
		int i;
		i = g() ? 1 : 0;
		return i;
	}

The value is not necessarily returned in a register: all you can be
sure of is that the value is returned.  The compiler is free to, as
Ron Natalie once put it, stuff arguments into an envelope and mail
them off to the function being called, and results can be returned
the same way.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list