C + + Debugging skills __c++

Source: Internet
Author: User

In C + +, a technique similar to header File Protection is sometimes used to perform debug code selectively. The basic idea is that the program can contain some code for debugging, but the code is only used when developing the program. When the application is written and ready to be published, the debug code is shielded first. This method uses two preprocessing functions: Assert and Ndebug.

assert preprocessing Macros
An assert is a preprocessing macro. A preprocessing macro is actually a preprocessing variable, which behaves somewhat like an inline function. An Assert macro uses an expression as its condition:

ASSERT (expr);

The first evaluation of expr, if the expression is false, asserts the output information and terminates the execution of the program. If the expression is true, Assert does nothing.

Assert macros are often used to check for conditions that cannot occur. For example, a program that operates on an input text may require that all the given words are longer than a certain threshold. At this point, the program can contain a statement that looks like this:

ASSERT (Word.size () >threshold)

ndebug preprocessing variables
The assert behavior depends on the state of a preprocessing variable named Ndebug. If Ndebug is defined, assert does nothing. Ndebug is not defined by default, and Assert performs a run-time check.

We can use a #define statement to define NDEBUG to turn off debug state.

Defining Ndebug avoids the run-time overhead of checking for various conditions, and of course it does not perform run-time checks at all. Therefore, assert should be used only to validate things that do not really happen. We can use assert as an aid to the debugger, but we cannot replace it with real run-time logic checks, nor do we replace the error checking that the program itself should contain.

In addition to using Assert, you can use Ndebug to write your own conditional debugging code. If Ndebug is not defined, the code between #ifndef and #endif is executed, and if Ndebug is defined, the code is ignored.

void Pring (const int ia[], size_t size)
{
    #ifndef  a local static variable defined by the Ndebug//__func__ compiler, for storing the name
    of the function Cerr << __func__ << ": Array size is" << size << Endl;
    #endlf
}

In addition to the C + + compiler definition, the preprocessor defines 4 other names that are useful for debugging:

name function
__func__ The name of the currently debugged function
__file__ string literal value that holds the file name
__line__ The integer literal value that holds the current line number
__time__ string literal value that holds the file compilation time
__date__ string literal value for file compilation date

As in the following example:

if (Word.size () < threshold)
    Cerr << "Error:" << __file__
         << ": in function" << __func __
         << "at line" << __line__ << endl
         << "Compiled on" << __data__
         << " At "<< __time__ << Endl;

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.