ANSI assert

Larry Jones scjones at thor.UUCP
Sun Sep 9 04:15:38 AEST 1990


In article <1428 at proto.COM>, joe at proto.COM (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).
> 
> [ Plauger's version which doesn't evaluate the argument when NDEBUG
>   is defined ]
> 
> 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);

I take it you haven't compiled this code with very many compilers.
As far as I know, assert has always been a macro, and there has
never been any guarantee that it's a safe one!  Your code is
highly non-portable.  X3J11 debated long and loud about whether
the argument should be evaluated when NDEBUG is defined, but the
decision was made that assert under NDEBUG should completely
disappear to avoid any performance hits, despite the fact that
you lose some error checking (i.e. the code might actually be
invalid if NDEBUG wasn't defined).  Code with side-effects in
an assert argument was generally agreed to be perverse at best.
----
Larry Jones                         UUCP: uunet!sdrc!thor!scjones
SDRC                                      scjones at thor.UUCP
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
It's going to be a long year. -- Calvin



More information about the Comp.std.c mailing list