ANSI assert

Doug Gwyn gwyn at smoke.BRL.MIL
Mon Sep 10 07:03:21 AEST 1990


In article <1428 at proto.COM> joe at 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 at 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 at 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 */
	}



More information about the Comp.std.c mailing list