http://www.java-samples.com/showtutorial.php?tutorialid=595
The macro assert() can diagnose program bugs. It is defined in ASSERT.H, and
its prototype is
The argument expression can be anything you want to test-a variable or any C expression. If expression evaluates to TRUE, assert() does nothing. If expression evaluate to FALSE, assert() displays an error message on stderr and aborts program execution.
How do you use assert()? It is most frequently used to track down program bugs (which are distinct from compilation errors). A bug doesn't prevent a program from compiling, but it causes it to give incorrect results or to run improperly (locking up, for example).
Analysis:
The action of assert() depends on another macro named NDEBUG (which stands for "no debugging"). If the macro NDEBUG isn't defined (the default), assert() is active. If NDEBUG is defined, assert() is turned off and has no effect. If you placed assert() in various program locations to help with debugging and then solved the problem, you can define NDEBUG to turn assert() off. This is much easier than going through the program and removing the assert() statements (only to discover later that you want to use them again). To define the macro NDEBUG, use the #define directive. You can demonstrate this by adding the line
#define NDEBUG
Note that NDEBUG doesn't need to be defined as anything in particular, as
long as it's included in a #define directive.
No comments:
Post a Comment