Just a minor new twist on free()

Ray Butterworth rbutterworth at watmath.waterloo.edu
Sat Oct 13 02:29:28 AEST 1990


In article <10066:Oct1212:30:5790 at kramden.acf.nyu.edu> brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
>Much better:
>  #define free(x) ( ( __frx = x ) , ( __frx ? __frol(__frx) : (void) 0 ) )
>  extern void *__frx;
>  extern void (*__frol)();

Which will blow up if an interrupt occurs and changes the value of __frx
between the assignment and the test, or the test and the __frol call.
(Considering the environment under which C was developed,
 it's amazing how non-reentrant so much of the library is.)

In article <4d5780ad.20b6d at apollo.HP.COM> blodgett at apollo.HP.COM (Bruce Blodgett) writes:
> #define free(x) { void * ptr = (x); if ( ptr != NULL ) free(ptr); }

You should call it FREE, but even then something like
"if (test) FREE(thing); else something_else();"
would give a syntax error.

Macros like that should be written as
#define NAME(arg) do { auto thing=arg; ... ; } while(0)
They still can't be used as expressions,
but at least they can be used in "if" statements.



More information about the Comp.lang.c mailing list