ANSI assert

Theo Norvell norvell at csri.toronto.edu
Sun Sep 9 06:48:57 AEST 1990


In article <1428 at proto.COM> joe at proto.COM (Joe Huffman) writes:
>
>either someone overlooked a potential problem or my coding
>style is going to have to be revised (as well as 10's of thousands of lines
>of code). 
> 
>Can anyone shed some light on this for me?
>
>-- 
>joe at proto.com
>FAX: 208-263-8772

No one overlooked this problem.  At the September 88 meeting, this very point
came up in response to a request from a prominent computer scientist who
argued much as do you.  It was decided that the least evil was to insist
that the argument not be evaluated when NDEBUG is not defined.

The reason is that it is quite reasonable for the argument to make no sense
when runtime checking is on.  For instance, it may use virtual variables,
variables that need not exist except for checking purposes, as in
	void root(void)
	{
	    #ifndef NDEBUG
		float x_init = x;
	    #endif
	    assert(x <= 0.0) ;
	    ... code to find square root ...
	    assert(abs(x_init - x*x) <= EPS) ;
	}
Note that root is neatly specified by pre and post conditions.

As has been pointed out, you may write your own assert macro.  May I
suggest

#define assert_and_do(x) ((x) || assert(0))

Theo Norvell                                         norvell at csri.toronto.edu



More information about the Comp.std.c mailing list