C++ debugging help

assert preprocessor macro

  assert is a preprocessing macro. The so-called preprocessing is actually a preprocessing variable that behaves like an inline function. The assert macro uses an expression as its condition:

assert(expr)

  The first is to evaluate expr, if its value is false, then assert output information and terminate the execution of the program, if it is true, do nothing . The assert macro is defined in the cassert header file . Because it is a macro, there is no scope or namespace, and it can be used directly. The namespace does not need to be included.

  Like preprocessing variables, macro names must be unique within the program. Programs containing cassert header files cannot define variables, functions, etc. named assert

 

NDEBUG preprocessing variable

  The behavior of assert depends on the state of a preprocessing variable named NDEBUG . If NDEBUG is defined, assert does nothing. By default, NDEBUG is not defined. At this time, assert performs runtime checks, which will compare squares.

Conveniently, when we don't need assert checks, we don't need to delete the previous assert statements one by one, just #define NDEBUG .

  In addition to using assert, we can also use NDEBUG to define our own debugging code, mainly to judge whether the NDEBUG preprocessing variable is defined in the code!

copy code
void print(const int ia[], size_t size) { #ifndef NDEBUG
 ........
    
      #endif } 
copy code

The C++ compiler provides the _ _func_ _ variable, we can output the name of the current debug function, and the preprocessor also defines the following names:

copy code
  _     _FILE_ _ // The string literal value that stores the file name
    _ _LINE_ _ // The integer literal value that stores the current line number    _ _TIME_ _ // The string literal value that stores the compilation time of the file    _ _DATE_ _ // The value that stores the compilation date string literal
copy code

This allows us to output more and more detailed information when defining our own debug code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324472861&siteId=291194637