return value of free

Walter Bright bright at Data-IO.COM
Sat Jan 28 04:48:48 AEST 1989


In article <287 at proton.UUCP> proton!nusbaum at ucrmath.ucr.edu (R. James Nusbaum) writes:
>What is the story on the return value of free?

ANSI C says that the return value of free() is (unfortunately) void. The
reason I say unfortunately is that it now can't return an error status.
Zortech's free() returns an int which says whether it failed or not. If
it failed, this means that either the heap has been corrupted or an
attempt was made to free a wild pointer. The proper behavior then is to
print a message and abort the program before it crashes. free() cannot
guarantee that it'll always detect errors, but it does often enough that
this is a valuable feature.

You could argue that free() should internally print the message and abort.
This is an inferior solution, because the program may need to do some
cleanup before it exits, like restoring hooked interrupt vectors and
switching the display back to text mode.

I intend to leave it this way, and in stdlib.h have something like:
	#if __STDC__
	void
	#else
	int
	#endif
		free(void *);

P.S. I wrote Zortech C.



More information about the Comp.lang.c mailing list