why is free() a void?

Richard Harter rh at smds.UUCP
Thu Oct 25 13:19:13 AEST 1990


In article <1749 at meaddata.meaddata.com>, rob at meaddata.com (Robert E. Lancia) writes:

	To summarize: Why is free a void function?  Why doesn't return
	something useful if the argument is invalid?

The answer is very simple -- the specs for malloc/free were broken in
the very beginning (IMHO, of course) and most implementations are broken
today.  It is easier (and faster) to put the allocation control information
in memory adjacent to the allocated block in allocators which use free
lists (bit mapped buddy-system allocators are a different matter.)

IMNSHO this is not nice.  It is a source of mystery bugs.  An array
overwrite in allocated memory can wipe out control information; the
consequence doesn't hit you until much later.  A stale or incorrect
pointer passed to free is allowed to do its damage without warning.
And so on.

The specs more or less explicitly provide that the users of malloc/free
have all of the responsibility for using them without error.  The
system is not obliged to do error checking.  It is at liberty to do
something indeterminately awful if the programmer makes an error.
All of this in one of the most primitive and least structured aspects
of C.  Grumble, grumble, grumble.

-- 
Richard Harter, Software Maintenance and Development Systems, Inc.
Net address: jjmhome!smds!rh Phone: 508-369-7398 
US Mail: SMDS Inc., PO Box 555, Concord MA 01742
This sentence no verb.  This sentence short.  This signature done.



More information about the Comp.lang.c mailing list