Xref: utzoo comp.lang.c:32825 comp.std.c:3754 Newsgroups: comp.lang.c,comp.std.c Path: utzoo!utgpu!watserv1!watmath!rbutterworth From: rbutterworth@watmath.waterloo.edu (Ray Butterworth) Subject: Re: Just a minor new twist on free() Message-ID: <1990Oct12.162928.21807@watmath.waterloo.edu> Organization: Math Faculty Computing Facility, University of Waterloo References: <4d5780ad.20b6d@apollo.HP.COM> <10066:Oct1212:30:5790@kramden.acf.nyu.edu> Date: Fri, 12 Oct 90 16:29:28 GMT Lines: 22 In article <10066:Oct1212:30:5790@kramden.acf.nyu.edu> brnstnd@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@apollo.HP.COM> blodgett@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.