ANSI assert

Joe Huffman joe at proto.COM
Sat Sep 8 15:57:57 AEST 1990


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).

----
/* assert.h standard header
 * copyright (c) 1990 by P.J. Plauger
 */
#undef assert/* remove any previous definition */
#ifdef NDEBUG

#define assert(test) ((void)0)

#else /* NDEBUG not defined */

void _Assert(char *);

#ifndef _STR /* define stringize macro just once */
#define _STR(x) #x
#endif

#define assert(test) ((test) || _Assert(__FILE__ ":" _STR(__LINE__) " " #test))
#endif
----

My concern is when NDEBUG is defined.  I have many places in my code where I
do something like the following:

  assert(i++ < limit);

  or

  assert(tree_init() == OKAY);

My definition of assert with debugging turned off looks like:

#define assert(test) ((void)(test))

Which allows the action inside the assert to occur.  Did the ANSI committee
overlook this?  Did P.J. Plauger?  Am I in error for doing this?  I realize
that macros can be hazardous (like #define SQR(x) ((x)*(x))) but this doesn't
have to be and yet 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



More information about the Comp.std.c mailing list