why is free() a void?

Walter Bright bright at nazgul.UUCP
Wed Oct 31 21:42:53 AEST 1990


In article <1749 at meaddata.meaddata.com> rob at pmserv.meaddata.com writes:
<As the subject says, I'm curious as to why free() does not return a value.
<It seems to me that it could pass back some useful information, especially
<if there was a problem.  Was the pointer NULL, or an invalid address??
<Was the memory not allocated by malloc() or it's siblings??

I used to feel that free() should return a value, basically indicating if
malloc's data structures were corrupted or not. After using this for years,
it became clear that the only reasonable thing to do is to abort the
program:
	if (free(p))
		abortprogram("heap is corrupted");
If the data structures are corrupted, it is best to terminate the program
as soon as possible, to avoid doing terrible things like corrupting DOS
or the FAT. Thus, it became apparent that the abort should be moved inside
of free(). That's what the Zortech free function does now, if it detects
a corrupted heap it prints the message:
	"Heap is corrupted"
and immediately terminates the program. free() now returns a void.

Occaisionally someone wants to regain control after this, so they can
'fail gracefully'. My argument is that if the heap is corrupted, the
program has already failed gracelessly, and it's best to terminate
it before more goes wrong. (The library source is available for
the diehards anyway!)



More information about the Comp.lang.c mailing list