Article View: comp.std.c
Article #1662Re: ANSI assert
From: Doug Gwyn
Date: Sun, 09 Sep 1990 21:03
Date: Sun, 09 Sep 1990 21:03
105 lines
2835 bytes
2835 bytes
In article <1428@proto.COM> joe@proto.COM (Joe Huffman) writes:
> assert(i++ < limit);
> assert(tree_init() == OKAY);
>My definition of assert with debugging turned off looks like:
>#define assert(test) ((void)(test))
Your definition of assert() is not standard conforming. When NDEBUG
is defined before inclusion of <assert.h>, the ONLY conforming definition
for the assert() macro is
#define assert(ignore) ((void)0)
I understand why you prefer your style, but X3J11 decided on the above.
Thus you need to change your coding style to something like
assert(i < limit);
++i;
or
#ifndef NDEBUG
assert(tree_init() == OKAY);
#else
(void) tree_init(); /* cast is optional; included for "lint" */
#endif
By the way, here's my public-domain implementation of section 4.2:
SOURCE FOR <assert.h> in the standard C compilation environment:
/*
<assert.h> -- definitions for verifying program assertions
public-domain implementation
last edit: 12-Apr-1990 Gwyn@BRL.MIL
complies with the following standards:
ANSI X3.159-1989
IEEE Std 1003.1-1988
SVID Issue 3 (except for the extra blank in the NDEBUG case)
X/Open Portability Guide Issue 3 (ditto)
*/
#undef assert
#ifdef NDEBUG
#define assert(ignore) ((void)0)
#else /* ! NDEBUG */
#ifdef __STDC__
extern void __assert(const char *, const char *, int);
#define assert(ex) ((ex) ? (void)0 : __assert(#ex, __FILE__, __LINE__))
#else /* ! __STDC__ */
extern void __assert();
/* Reiser CPP behavior assumed: */
#define assert(ex) ((ex) ? (void)0 : __assert("ex", __FILE__, __LINE__))
#endif /* __STDC__ */
#endif /* NDEBUG */
SOURCE FOR __assert() in the standard C run-time support library:
/*
__assert() -- support function for <assert.h>
public-domain implementation
last edit: 16-Jan-1990 Gwyn@BRL.MIL
complies with the following standards:
ANSI X3.159-1989
IEEE Std 1003.1-1988
SVID Issue 3
X/Open Portability Guide Issue 3
*/
#include <stdio.h>
extern void abort();
#ifndef __STDC__
#define const /* nothing */
#endif
void
__assert(expression, filename, line_num)
const char *expression, *filename;
int line_num;
{
(void) fprintf(stderr, "assertion failed: %s, file %s, line %d\n",
expression, filename, line_num);
(void) fflush(stderr);
abort();
/* NOTREACHED */
}
Technical Details
Message-ID:
<13778@smoke.BRL.MIL>Path:
arc1-ca.pugleaf.net!utzoo!attcan!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!wuarchive!uunet!aplcen!haven!adm!smoke!gwynReferences:
<1428@proto.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!wuarchive!uunet!aplcen!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.std.c Subject: Re: ANSI assert Keywords: assert, NDEBUG Message-ID: <13778@smoke.BRL.MIL> Date: 9 Sep 90 21:03:21 GMT References: <1428@proto.COM> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 105