Differences between assert verify trace and Its Application

Source: Internet
Author: User

The assert () macro is used to ensure that a specific condition is met. The usage is:
Assert (expression );
If the expression value is false, the entire program exits and an error message is output. If

If the value is true, continue to execute the following statement.
Before using this macro, the header file assert. h must be included.
For example
# Include <stdio. h>
# Include <assert. h>
Void main ()
{
Float a, B;
Scan ("% F", & A, & B );
Assert (B! = 0 );
Printf ("% F/N", A/B );
}
The above program needs to calculate the value of a/B, So B is required! = 0, so assert () is used in the program ()

Used to ensure B! = 0. If B = 0, the program exits.
Two versions of DEBUG are available in VC. Release. Assert () is only used in debuge and used for testing.

Whether the expressions in parentheses are true. For example, each number x cannot be 0.

Write assert (x); but this statement does not generate code to check this statement during your release.

Value, which takes effect only when debug is enabled.
Assert () is a judgment statement used to debug programs. Detailed description of assert (

Booleanexpression );
1. The booleanexpression parameter is a bool expression.
2. When the program runs the Statement, the program checks the expression booleanexpression.

Is it true or false.
When the conditions are met, the program continues to run the following code;
When the conditions are not met, the program will be stuck here for continuous running, not running down, and

The program prompts an error dialog box, indicating that the booleanexpression condition is not met.

Errors.
3. This command can be used as a protection for certain conditions to avoid importing non-conforming items.

Program crash.

The role of assert and verify Macros in debug mode is basically the same.

For row calculation, if the value is not 0, nothing is done. If the value is 0, the diagnosis information is output.

However, the effect of the two in the release mode is completely different. Assert does not calculate the expression value,

The diagnostic information is not output. Verify calculates the value of the expression, but neither the value nor the value is 0.

The diagnostic information is output.
 
Assertion is executed in a dialog box with assertion information (Program, module, and assertion rows ).

The dialog box has three buttons: "break", "repeat" ("debug"), and "continue"

("Ignore"). "break" ends the program, "continue" ignores assertions, the most useful

Is the "repeat" button. Press it to open the source code editor in the asserted place. Here you can

Test all the variable values and understand what went wrong.
Example: assert (ppointer); Assert (n> 0 & n <100); Assert (0 );
Assert is useful for simple verification, but it is useful for C ++ objects, especially cobject-derived

, There is a better method assert_valid to implement similar operations. As a general rule,

We should check the data error before starting to use each object,
The assert_valid macro makes it easy to implement the cobject derived class.
Example: assert_valid (this); assert_valid (pview );
Verify ()
Verify () and assert () are very similar. The difference is that in the release version, it still has

Effect

Evaluate the parameter expression ).
Trace ()
Trace () is basically a replica of the printf () function. The only difference is that

The result is output to the debugging window. In the release version, it is also invalid. Generally

Trace0 (), trace1 (), trace2 ()... Instead of trace ().
? These three macros do not have any material impact in the release version. Are they

The function depends on whether macro _ debug is predefined. This is for Microsoft Visual

In C ++, other compilers may have different macros.
Common usage:
To control the passed pointer:
Void somefun (sometype * ppointer)
{
Assert (ppointer );
// Some instructions.
}
You can capture strange values in the "Switch" and "if" operations.
For example:
Switch (nrgbcolors ){
Case nred: {// some instructions.} break;
Case ngreen: {// some instructions.} break;
Case nblue: {// some instructions.} break;
Default: assert (0); // We shoshould have never come here!
}
If (nwatertemp> = 0 & nwatertemp <50 ){
// Some instructions.
}
Else if (nwatertemp> = 50 & nwatertemp <= 100 ){
// Some instructions.
}
Else {
Assert (0); // We shoshould have never come here!
}
Assertion of the value:
Assert (nsomevalue> = minvalue and nsomevalue <= maxvalue );
Assert (nothervalue! = 0 );
 
Cute assert Error
Assert (m_mywnd.create ());
Vomit! This is a terrible mistake! The program works normally in the debugging version and does not work in the release version.

Remember: This is a macro that will be removed from the release. In this way, your window will always be

Will not be created. If you use MFC, do this:
Verify (m_mywnd.create ());
It runs m_mywnd.create () in the debugging version just like assert in the release version ().

In debug mode:
When the value of the test is 0, assert prompts an error and interrupts program execution. Verify will also

An error is reported, but the program execution is paused.

In release mode:
The assert statement is ignored, as if it does not exist. The verify statement itself is not executed, but its parentheses

The statement in can be executed, and the result value is available. Because verify is not executed, no error is reported.

Do not pause program execution.

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.