An in depth look at the Assert of assertion implementation mechanism

  • 2020-04-02 01:29:32
  • OfStack

Assert is used to determine if a program is running correctly and to ensure that it behaves as we understand it. It is called assert(logic expression) and abort() to abort the program if the logical expression is false.

Viewing the MSDN help document, you can get the explanation information for assert as follows:


The ANSI assert macro is typically used to identify logic errors during program development, by implementing the expression argument to evaluate to false only when the program is operating incorrectly. After debugging is complete, assertion checking can be turned off without modifying the source file by defining the identifier NDEBUG. NDEBUG can be 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.

An assert identifies a logical error in a program by determining whether its arguments are true or false. After debugging, you can turn off an assert by defining NDEBUG.

Check the include/assert. H header file for macros related to assert 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  

Explanation:

  #ifdef NDEBUG 
      #define assert(_Expression)  ((void)0)//When debugging is complete, turn off the assertion and optimize the generated code if NDEBUG is defined

The following code defines the following function (which is used to print out the error message) :

_wassert(_In_z_ const wchar_t * _Message, _In_z_ const wchar_t *_File, _In_ unsigned _Line);

Interested to see its implementation in assert. C, the function first determines the error reporting mode and the type of program (console or GUI) to determine whether assert prints to standard error or appears as a message box, and then calls abort () to abort the program. Extern "C" has time to explain

Okay, finally, you see the macro definition for assert


#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )

If interpretation _Expresssion is false, then! False = true,! True =false, then continue to execute the statement after ||, so the error message will be printed and the program will be terminated. If _Expression is true, then! True = false,! False =true, the statement after || is no longer executed, so no information is printed out.

It is worth noting that there is a comma expression, interested can be studied, the comma expression is as follows


(_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0)

The result of the asset assertion is always void(1)/void(0) because of the comma expression.

The role of Assert in a program

Examples of Assert:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/20130913102328.png ">

Explanation: Since TMP =0 and TMP ==1 is false, the argument to assert macro when the program runs is false, so the result of the call is to print an error message to stderr and then abort to abort the program. If you change to TMP =1, the program works perfectly. If you want to turn off an assert macro assertion in your program, you can do this by defnie NDEBUG

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/20130913102343.png ">  
You will find that even if TMP =0, the assertion information will no longer appear, please see the explanation at the top

Function:
1: assertions can be used to check the validity of passed function arguments


void max(int *a, int n)
{
 assert(a!=null)//Use assertions to ensure that the argument passed to the function is not a null pointer
}

2: an assertion is generally used to check only one condition to facilitate the analysis of the program [written by the master] < < The programming of mission > > The art of assertion an assertion can && and || several conditions, before we are not a master, it is best not to do this ~~~

3: the assertion before and after the best space [programming style problems, according to your own preferences, the best for yourself]

4: assertions are only used to check the logical correctness of the program, and cannot replace conditional substitution

5: assertions are better than printf statements

6: assert arguments can be function calls, but functions return true or false values, such as assert(sort())


Related articles: