Just a minor new twist on free()

Conor P. Cahill cpcahil at virtech.uucp
Tue Oct 2 23:23:13 AEST 1990


In article <PDS.90Oct1095707 at lemming.webo.dg.com> pds at lemming.webo.dg.com (Paul D. Smith) writes:
>It should be pointed out that in ANSI C free(NULL) is defined, and is
>legal (any conforming implementation of free() must ignore being
>passed a NULL pointer).

While this is a true statement, I would never recommend that one take advantage
of this feature.  If you know the variable is NULL don't pass it to free.

The performance cost of the following statement:

		if( ptr != NULL )
			free(ptr);

as opposed to:

		free(ptr);

will be unmeasurable in most, if not all, circumstances.  In addition, by
adding the if() you get code that is portable across all implementations.

Anyway, this is really a moot point because you should never be calling
free() unless you KNOW WHAT IS IN THE PTR that you will be passing to it.
If you don't, then something is wrong with your code.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.lang.c mailing list