Article View: comp.std.c
Article #1712I assert Plauger's header is wrong
From: D. Hugh Redelmeier
Date: Sat, 15 Sep 1990 05:07
Date: Sat, 15 Sep 1990 05:07
55 lines
2026 bytes
2026 bytes
In article <1428@proto.COM> joe@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).
>
>----
>/* 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
I think that there are (at least) three bugs in PJP's assert.h:
1. || does not accept void on its right-hand side, but PJP puts a
call to _Assert on the right-hand side of ||, and _Assert is
defined to return void.
2. assert's return type should be void, yet PJP's assert returns the
result of ||, an int.
3. In ANSI C, _STR(__LINE__) would yield "__LINE__", not,
say, "937". To get what he wants, he needs to use xstr from pg 93
of the standard.
Sample:
#define str(s) #s
#define test1 str(__LINE__)
#define xstr(s) str(s)
#define test2 xstr(__LINE__)
test1 /* yields "__LINE__" */
test2 /* yields "9" */
Hugh Redelmeier
{utcsri, yunexus, uunet!attcan, utzoo, scocan}!redvax!hugh
When all else fails: hugh@csri.toronto.edu
+1 416 482-8253
Technical Details
Message-ID:
<1990Sep15.010753.23079@jarvis.csri.toronto.edu>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!snorkelwacker!usc!cs.utexas.edu!news-server.csri.toronto.edu!dgp.toronto.edu!hughReferences:
<1428@proto.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!snorkelwacker!usc!cs.utexas.edu!news-server.csri.toronto.edu!dgp.toronto.edu!hugh From: hugh@dgp.toronto.edu (D. Hugh Redelmeier) Newsgroups: comp.std.c Subject: I assert Plauger's header is wrong Keywords: assert, NDEBUG Message-ID: <1990Sep15.010753.23079@jarvis.csri.toronto.edu> Date: 15 Sep 90 05:07:53 GMT References: <1428@proto.COM> Organization: CSRI, University of Toronto Lines: 55