On Thu, 2007-01-18 at 13:20 -0800, Micah Cowan wrote:

> #define CHECK_FOR_NULLITITY(var) \
>       if ( (var) == NULL ) \
>       { \
>               printf("%s is null.\n", #var); \
>               exit(EXIT_FAILURE); \
>       }

Forgot to point out, that since the # stringizing operator produces a
string literal, you might make things just /slightly/ more efficient by
taking advantage of C's string literal concatenation:

#define CHECK_FOR_NULLITITY(var) \
        if ( (var) == NULL ) \
        { \
                puts( #var " is null." ); \
                exit(EXIT_FAILURE); \
        }

Also, in real applications you might consider printing that to stderr,
since that's where most folks expect error logging to happen--and
especially if you put other, legit text on stdout.

In case you feel like playing around with the # operator, be aware that
the stringizing operation takes place before macros within arguments are
expanded recursively. So, if you were to try something like:

#define MY_EXPRESSION (__foo->special_ptr)

CHECK_FOR_NULLITY(MY_EXPRESSION) would print out "MY_EXPRESSION is
null", rather than "(__foo->special_ptr) is null". To fix this, you can
force the stringization to happen after the parameter has been expanded,
with something like:

#define DO_STR(a) #a
#define CHECK_FOR_NULLITITY(var) \
    ... /* use DO_STR(var) instead of #var */ ...

(Oh, hey, just noticed there's too many ITs in NULLITITY :) )
This will expand out any macros in var before using it as the argument
to the DO_STR() macro.

-- 
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/

_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech

Reply via email to