Just a minor new twist on free()

Bruce Blodgett blodgett at apollo.HP.COM
Fri Oct 12 08:55:00 AEST 1990


If you are trying to compile code which may call free() with a NULL
argument, and use an implementation of free() that does not handle NULL
gracefully, try adding one of the following to <stdlib.h>:

#define free(x) ( (x) ? free(x) : ( (void)0 ) )
  or
#define free(x) { void * ptr = (x); if ( ptr != NULL ) free(ptr); }

The former has the side-effect of evaluating the argument to free()
twice.  The latter is a statement rather than an expression of type
void, and therefore wouldn't work if someone tried to embed a call to
free() in an expression (there are not many uses for subexpressions of
type void).
Bruce Blodgett
blodgett at apollo.hp.com
(508) 256-0176 x4037



More information about the Comp.lang.c mailing list