Assert (Assert Implementation Mechanism Analysis)

Source: Internet
Author: User

Assert is used to determine the correctness of the program running and ensure that the program running behavior is consistent with what we understand. The call form is assert (logic expression). If the logic expression is false, abort () is called to terminate the program.

View the MSDN help document and get the following explanation for assert:


[Cpp]
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 direve ve. if NDEBUG is defined with # define, the directive must appear before ASSERT. H is supported ded.

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 direve ve. if NDEBUG is defined with # define, the directive must appear before ASSERT. H is supported ded.
Assert identifies the logic error of the program by judging whether its parameters are true or false. After debugging, you can define NDEBUG to disable assert assertions.

View the include/assert. h header file to obtain the macro write meanings related to assert as follows:


[Cpp]
# Ifdef NDEBUG
 
# Define assert (exp) (void) 0)
 
# Else
 
# Ifdef _ cplusplus
Extern "C "{
# Endif
 
_ Cribd void _ cdecl _ assert (void *, void *, unsigned );
 
# Ifdef _ cplusplus
}
# Endif
 
# Define assert (exp) (void) (exp) | (_ assert (# exp, _ FILE __, _ LINE _), 0 ))
 
# Endif/* NDEBUG */

# Ifdef NDEBUG

# Define assert (exp) (void) 0)

# Else

# Ifdef _ cplusplus
Extern "C "{
# Endif

_ Cribd void _ cdecl _ assert (void *, void *, unsigned );

# Ifdef _ cplusplus
}
# Endif

# Define assert (exp) (void) (exp) | (_ assert (# exp, _ FILE __, _ LINE _), 0 ))

# Endif/* NDEBUG */
Explanation:

[Cpp]
# Ifdef NDEBUG
# Define assert (_ Expression) (void) 0) // After the debugging is complete, if NDEBUG is defined, disable the assertions to optimize the generated code

# Ifdef NDEBUG
# Define assert (_ Expression) (void) 0) // After the debugging is complete, if NDEBUG is defined, disable the assert, optimize the generated code. The following Code defines the following function (this function is used to print the error message ):

[Cpp]
_ Wassert (_ In_z _ const wchar_t * _ Message, _ In_z _ const wchar_t * _ File, _ In _ unsigned _ Line );

_ Wassert (_ In_z _ const wchar_t * _ Message, _ In_z _ const wchar_t * _ File, _ In _ unsigned _ Line); if you are interested, you can click assert. c shows its implementation. The function first needs to determine the reporting mode of errors and the program type (console program or GUI program) whether the assert is printed to the standard error output or in the form of a message box, finally, the abort () function is called to terminate the program. There is time to explain the extern "C"

Now, we finally see the macro definition of assert.


[Cpp]
# Define assert (_ Expression) (void )((!! (_ Expression) | (_ wassert (_ CRT_WIDE (# _ Expression), _ CRT_WIDE (_ FILE _), _ LINE _), 0 ))

# Define assert (_ Expression) (void )((!! (_ Expression) | (_ wassert (_ CRT_WIDE (# _ Expression), _ CRT_WIDE (_ FILE _), _ LINE _), 0 ))
Interpreted _ Expresssion if it is false, then! False = true ,! True = false. Execute the statement again. | if _ Expression is true, an error message is printed and the program is terminated! True = false ,! False = true. The statement is not executed at this time. | subsequent statements are not printed.

It is worth noting that there is a comma expression in it. If you are interested, you can study it. The comma expression is as follows:


[Cpp]
(_ Wassert (_ CRT_WIDE (# _ Expression), _ CRT_WIDE (_ FILE _), _ LINE _), 0)

(_ Wassert (_ CRT_WIDE (# _ Expression), _ CRT_WIDE (_ FILE _), _ LINE _), 0)
The result returned after asset assertion is always void (1)/void (0), because it is a comma expression.

Role of Assert assertions in programs

Example of Assert:

 


Explanation: Because tmp = 0, tmp = 1 is false, the parameter passed to the assert Macro during program running is false. Therefore, the call result is to print an error message to stderr first, then, terminate the program by calling abort. If it is changed to tmp = 1, the program runs completely normally. For example, if you want to disable assert macro assertions in a program, you can do the following: defnie NDEBUG

 


You will find that tmp = 0 and no asserted information is displayed. For more information, see the top part.

Purpose:

1: assertions can be used to check the validity of the parameters passed to the function.

[Cpp]
Void max (int * a, int n)
{
Assert (! = Null) // Use assertions to ensure that the parameter passed to the function is not a null pointer.
}

Void max (int * a, int n)
{
Assert (! = Null) // Use assertions to ensure that the parameter passed to the function is not a null pointer.
} 2: an assertion is generally used to check only one condition, so that it is easy for the analysis program to [the art of the <programming Pearl> assertion written by the master can be an assertion & | several conditions, we 'd better not do this before we are masters ~~~]

3: it is best to leave a space before and after assertions [programming style issues, according to your own preferences, suitable for yourself is the best]

4: assertions are only used to check the logic correctness of the program and cannot be replaced by conditions.

5: assertion is better than printing in the form of printf statements ~~~~

6: The asserted parameter can be called by a function, but the return value of the function is true or false, such as assert (sort (). For more information, see the source code analysis above.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.