The assertion (assert) is used to determine the correctness of the program's operation and to ensure that it behaves in a consistent and understandable order. The invocation form is assert (logic expression), and if the logical expression is false, the abort () terminates the running of the program.
To view the MSDN Help documentation, you can get an assert explanation as follows:
Copy Code code as follows:
The ANSI assert macro is typically used to identify logic errors during program development, by implementing the Expressio n argument to evaluate to false as the program is operating incorrectly. After debugging are complete, assertion checking can be turned off without modifying the source file by defining the Identi Fier Ndebug. Ndebug can is defined with a/d command-line option or with a #define directive. If ndebug is defined with #define, the directive must appear before ASSERT. H is included.
The general meaning is that assert is to identify the logic errors of the program by judging the true and false of its parameters, after debugging can be defined ndebug to turn off assert assertion.
To view the include/assert.h header file, you can get an Assert-related macro written as follows:
Copy Code code as follows:
#ifdef Ndebug
#define ASSERT (exp) ((void) 0)
#else
#ifdef __cplusplus
extern "C" {
#endif
_crtimp void __cdecl _assert (void *, void *, unsigned);
#ifdef __cplusplus
}
#endif
#define ASSERT (exp) (void) (exp) | | (_assert (#exp, __file__, __line__), 0)
#endif/* Ndebug * *
Explain:
Copy Code code as follows:
#ifdef Ndebug
#define ASSERT (_expression) ((void) 0)//When debugging is complete, if ndebug is defined, the assertion is closed, and the generated code is optimized
The next code means defining the following function (this function is used to print out an error message):
Copy Code code as follows:
_wassert (_in_z_ const wchar_t * _message, _in_z_ const wchar_t *_file, _in_ unsigned);
Interested in the ASSERT.C can see its implementation, the function first decides whether the wrong report pattern and the type of program (console or GUI) determines whether the assert is printed to the standard error output or the form of a message box, and finally calls the Abort () function to terminate the program's operation. for extern "C" to have time to explain
Well, in the end, I finally see the Assert macro definition
Copy Code code as follows:
#define ASSERT (_expression) (void) (!! (_expression)) || (_wassert (_crt_wide (#_Expression), _crt_wide (__file__), __line__), 0)
Explain _expresssion if False, then!false=true,!true=false, continue execution at this time | | Later statements, it will print out the error message, terminate the program, if _expression is true, then!true=false,!false=true, no longer execute | | Subsequent statements, so no information is printed.
It is noteworthy that there is a comma expression, interested to study, the comma expression is as follows
Copy Code code as follows:
(_wassert (_crt_wide (#_Expression), _crt_wide (__file__), __line__), 0)
The result returned after the asset assertion is always void (1)/void (0), because of the comma expression.
Assert assertions in the role of the program
Example of an Assert:
Explanation: because Tmp=0,tmp==1 is false, the argument to the Assert macro is false when the program runs, so the result of the call is to print an error message to stderr and then terminate the program by calling abort. If you change to Tmp=1, the program is fully functional. If you want to turn off Assert macro assertions in your program, you can defnie the following ndebug
You will find that there is no tmp=0, no assertion information, please look at the top
Role:
1: Assertions can be used to check the legality of the parameters passed to the function
Copy Code code as follows:
void Max (int *a, int n)
{
ASSERT (A!=null)//Use assertion to ensure that the argument passed to the function is not a null pointer
}
2: An assertion is generally only used to check a condition that facilitates the analysis of the program "master writes << programming Zhuji >> asserts the art of an assertion can be && with | | Several conditions, before we are not masters, it is best not to do so ~ ~ ~
3: The best space before and after the assertion [programming style problems, according to your own preferences, suitable for their own best]
4: Assertion is only used to check the logic correctness of the program, can not substitute for conditional replacement
5: Assertion is better than printf statement in this form of printing ~ ~ ~
6: Assertion argument can be a function call, but the function return value if TRUE or false, such as assert (sort ()), explain the above source analysis