'assert' peeves [was: failure due to compiler?]

hagerman@ece.cmu.edu (John Hagerman)
27 Jul 1996 21:42:11 -0400

          From comp.compilers

Related articles
Re: failure due to compiler? kanze@lts.sel.alcatel.de (1996-07-04)
Re: failure due to compiler? gah@u.washington.edu (1996-07-19)
Re: failure due to compiler? jgllgher@maths.tcd.ie (Dara Gallagher) (1996-07-20)
Re: failure due to compiler? davidg@genmagic.com (1996-07-24)
Re: failure due to compiler? jmccarty@sun1307.spd.dsccc.com (1996-07-26)
'assert' peeves [was: failure due to compiler?] hagerman@ece.cmu.edu (1996-07-27)
| List of all articles for this month |

From: hagerman@ece.cmu.edu (John Hagerman)
Newsgroups: comp.compilers
Date: 27 Jul 1996 21:42:11 -0400
Organization: Carnegie Mellon University
References: 96-07-041 96-07-123 96-07-141 96-07-173 96-07-180
Keywords: design, debug

jmccarty@sun1307.spd.dsccc.com (Mike McCarty) writes:
> [re: double eval of assert predicate]
> Yes, that is a common flaw in <assert.h> implementations. DEC shipped
> such a defective <assert.h> until I complained. Here is one which
> I believe works. ...


I dislike the assert "feature" of printing the predicate. This causes
problems when the predicate has complex line structure (example below, using
Mike's assert). I no longer use vendor-provided asserts because this
mis-feature has given me so much trouble. The standard assert in "old" C
didn't have this mis-feature, and I don't understand why it is needed: if
you get an assertion failure as a user then printing the predicate isn't
going to help; if you get an assertion failure as a programmer then you will
be going to the code directly -- printing the predicate leads to bad assert
usage habits.


In the example below I have fixed a couple of typos in Mike's assert.
Compiling this with gcc 2.7.2 on AIX 4.1 and running gives:


foobar
assertion failed: t.c:17: foo("foo\ar\n", 0)Abort process (core dumped)


This is just one example; every "ANSI C" assert I've seen has had a
problem printing some complicated predicate, and since I don't think
the predicate should be printed I just avoid them all.


~ John




#include <stdio.h>


#define __ASSERT(x) ((void)(fputs("assertion failed: ",stderr),fputs(x,stderr),abort(),0))
#define __STR__(x) __VAL__(x)
#define __VAL__(x) #x
#define assert(x) ((x)?(void)0:__ASSERT(__FILE__ ":" __STR__(__LINE__) ": " #x))


int foo(char *a, int b)
{
        fputs(a, stderr);
        return b;
}


int main(void)
{
        assert(foo("foo\
bar\n", 0));
}


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.