l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
November 4: Social gathering
Next Installfest:
TBD
Latest News:
Oct. 24: LUGOD election season has begun!
Page last updated:
2007 Jan 19 12:24

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] C preprocessor - "soft reference"?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] C preprocessor - "soft reference"?



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



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.