ANSI assert

Karl Heuer karl at haddock.ima.isc.com
Wed Sep 19 05:08:41 AEST 1990


In article <8795 at cognos.UUCP> jimp at cognos.UUCP (Jim Patterson) writes:
>In article <1990Sep8.164857.2930 at jarvis.csri.toronto.edu> norvell at csri.toronto.edu (Theo Norvell) writes:
>>#define assert_and_do(x) ((x) || assert(0))
>
>There's a slight problem with this. While it will work, it will print
>a rather misleading message...

The more serious problem is that it won't work, since void expressions can't
be operands of `||'.

>It would be better, though more work and potentially less portable, to copy 
>and modify the definition of assert().

Alternately:
	#if defined(NDEBUG)
	#define require(expr) ((void)(expr))
	#else
	#define require(expr) assert(expr)
	#endif
Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint
________
Exercise for the student: does the error message now contain the user's
string or the constant "expr"?  Prove your answer using 3.8, then check your
favorite ANSI compiler to see if it agrees.  Be prepared to defend your
reasoning in the form of a bug report.



More information about the Comp.std.c mailing list