Assert, assert_valid, verify, trace usage

Source: Internet
Author: User
Assert ()
  The assert () parameter is tested. If the parameter is 0, the execution is interrupted and a description message is printed. It does not play any role in the release version program. When using assert (), you must ensure that there is no function call in the parameter expression (Note: assert () macro does not evaluate the expression in the release version ), therefore, the macro verify () should be used for any parameter expressions with function calls to ensure that function calls in the expressions are correctly evaluated in the release version.
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, and the most useful is the "repeat" button. press it to open the source code editor in the assertion. 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. However, for C ++ objects, especially objects derived from cobject, there is a better method assert_valid to perform 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 () is very similar to assert (), but the difference is that it is still valid in the release version (note: the original author did not make it clear here, verify () will not print the description, only evaluate the parameter expression ).
Trace ()
  Trace () is basically a replica of the printf () function. The only difference is that it outputs the result to the debugging window. In the release version, it is also invalid. Trace0 (), trace1 (), trace2 ()... Instead of trace ().
These three macros do not have any material impact in the release version. Whether they work depends on whether the predefined macro _ debug is defined. This is a different macro in other compilers for Microsoft Visual C ++.
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 never be created. if you use MFC, do the following:
Verify (m_mywnd.create ());
It runs m_mywnd.create () in the debugging version just like assert in the release version ().

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.