Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!aussie!rex From: rex@aussie.UUCP (Rex Jaeschke) Newsgroups: comp.std.c Subject: Re: ANSI assert Message-ID: <44.UUL1.3#5077@aussie.UUCP> Date: 8 Sep 90 16:33:58 GMT References: <1428@proto.COM> Organization: Journal of C Language Translation Lines: 69 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 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 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) ----------------------------------------------------------------------------