ANSI assert

Rex Jaeschke rex at aussie.UUCP
Sun Sep 9 02:33:58 AEST 1990


Joe Huffman writes

> In the latest version of 'The C User Journal' P.J. Plauger wrote about the
> assert 'function' under ANSI C.  He outlined various 'sins' of many 
> implementations of assert and 'blessed' the following version.  It appears
> to me that this is wrong (it would break nearly all of my code).

You raise an interesting point. However, the standard explicitly states 
that if NDEBUG is defined, assert is defined simply as

	#define assert(ignore) ((void)0)

This implies that any side-effects present in the argument are NOT 
seen since the argument expression is NOT evaluated. This appears to 
be confirmed by the fact that the name of the macro's formal parameter 
is `ignore.'

Since assert was done several meetings before I started on X3J11 
perhaps someone else can remember whether this was deliberate.


I wrote an article much the same as Plauger's in the Dec issue of The 
Journal of C Language Translation (Vol 1, #3). Here's a small test 
program (from that article) to check your version of assert.h.

---------------------------------------------------------------------
To check your version, compile the following test program.  No errors
should be produced.

#define NDEBUG
#include <assert.h>

void f(int i)
{
	assert(i);

	i ? assert(i - 4) : assert(i + 4);

	if (i > 24)
		assert(i * 3);
	else
		assert(i * 24);
}

#undef NDEBUG
#include <assert.h>

void g(int i)
{
	assert(i);

	i ? assert(i - 4) : assert(i + 4);

	if (i > 24)
		assert(i * 3);
	else
		assert(i * 24);
}
---------------------------------------------------------------------

Rex

----------------------------------------------------------------------------
Rex Jaeschke     |  Journal of C Language Translation  | C Users Journal
(703) 860-0091   |        2051 Swans Neck Way          | DEC PROFESSIONAL
uunet!aussie!rex |     Reston, Virginia 22091, USA     | Programmers Journal
----------------------------------------------------------------------------
Convener of the Numerical C Extensions Group (NCEG)
----------------------------------------------------------------------------



More information about the Comp.std.c mailing list