return value of free

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Jan 28 14:11:54 AEST 1989


In article <1849 at dataio.Data-IO.COM> bright at dataio.Data-IO.COM (Walter Bright) writes:
>I intend to leave it this way, and in stdlib.h have something like:
>	#if __STDC__
>	void
>	#else
>	int
>	#endif
>		free(void *);

But this is wrong, except perhaps for Zortech C.  free() traditionally
never did return a value, although before "void" was added to C there
was no way to express this.

Note that none of the standard C library routines are specified with
provisions for indicating programmer usage errors.  This does not mean
that defensive programming is bad, merely that the burden for safety
checking was deemed better taken on explicitly by applications than by
the standard library.  For example, you could implement your own "safe"
alloc/dealloc routines as wrappers around malloc()/free().  In fact we
have done that for a large programming project I'm currently involved
with.  This technique has the advantage that time-consuming exhaustive
validation can be performed under optional debugging control, and when
a usage error is detected the error handling can be just what we want
it to be (we also have a special package for logging errors).  For such
a thorough job of usage validation, anything extra done in the standard
library would be a total waste of time.

For less careful applications, cheap tests for null pointers, etc. can
and probably should be done in the implementation of the standard
library.  Better to get an assert() failure during development than to
have the code run amok in unpredictable ways during production use.
But I don't think the standard library is any place to implement more
thorough safety checks.  Applications that want them will provide their
own, and know that their availability will not depend on the whims of
the C implementors.



More information about the Comp.lang.c mailing list