Question about assertion macro
Don McCrady
mccrady at torolab6.vnet.ibm.com
Mon Jun 3 23:11:34 AEST 1991
>>From: greg at suntan.viewlogic.com (Gregory Larkin)
>>I would like to construct an assertion macro so that I
>>can print the exact condition that failed as a string.
>> ASSERT(foo != NULL, "Unexpected NULL pointer");
>>Greg Larkin (ASIC Engineer)
> #define ASSERT_ARG(relation, msg, action) \
> if ( !(relation) ) { \
> printf("ASSERTION FAILED: File %s Line %d %s %s\n",\
> __FILE__, __LINE__, "relation", msg);\
> action; \
> } /* if assertion failed */
>
> should work in standard C; I'm not sure about ANSI C, though.
> From: msh30 at ruts.ccc.amdahl.com (Mark Hahn)
No, this won't work in ANSI C... If you use an ANSI C preprocessor,
you can do this easily with the "stringize" operator, #... Try this:
#define ASSERT_ARG(relation, msg, action) \
if ( !(relation) ) { \
printf("ASSERTION FAILED: " #relation "File %s Line %d %s\n",\
__FILE__, __LINE__, msg); \
action(); \
}
Better yet, if you have an ANSI compiler, you should have a header
file called <assert.h>, which will do exactly what you want; that
is, print the failing assertion, the file and line number, and call
the abort() function to terminate the program.
Enjoy.
++Don;
More information about the Comp.lang.c
mailing list