Introduction to the use of assert() in Linux C

      assert() is a macro that is often used when debugging a program. It evaluates the expression enclosed in parentheses when the program is running. If the expression is FALSE (0), the program will report an error and terminate execution. If the expression is not 0, continue to execute the following statement, its role is to terminate the program to avoid serious consequences, but also to find errors .

Required header file: #include <assert.h>

Conditions for reporting errors: assert(0);
[cpp]  view plain copy  
  1. void assert(     
  2.    int expression     
  3. );    

Parameters: Expression (including pointers) that evaluates to nonzero or 0.

Principle: The role of assert is to evaluate the expression expression, if its value is false (ie, 0), then it first prints an error message to stderr, and then terminates the program by calling abort.

Sample program:

[cpp]  view plain copy  
  1. void *memcpy(void *pvTo, constvoid *pvFrom, size_t size)   
  2. {  
  3.     assert((pvTo != NULL) && (pvFrom != NULL));    // use assert  
  4.     byte *pbTo = (byte *) pvTo;                    // prevent changing the address of pvTo  
  5.     byte *pbFrom = (byte *) pvFrom;                // prevent changing the address of pvFrom  
  6.     while(size -- > 0 )  
  7.     * pbTo ++ = * pbFrom ++;  
  8.       
  9.     return pvTo;  
  10. }  


assert() usage summary:

1. Check the validity of the incoming parameters at the beginning of the function

Such as:

[cpp]  view plain copy  
  1. int resetBufferSize(int nNewSize)  
  2. {  
  3.     //Function: change the buffer size,  
  4.     //Parameter: nNewSize new length of buffer  
  5.     //Return value: the current length of the buffer  
  6.     //Description: keep the original information content unchanged nNewSize<=0 means clear the buffer  
  7.     assert(nNewSize >= 0);  
  8.     assert(nNewSize <= MAX_BUFFER_SIZE);  
  9.   ...  
  10. }  

2. Each assert only tests one condition, because when multiple conditions are tested at the same time, if the assertion fails, it is impossible to intuitively judge which condition fails

不好: assert(nOffset>=0 && nOffset+nSize<=m_nInfomationSize);

好: assert(nOffset >= 0);
     assert(nOffset+nSize <= m_nInfomationSize);


3. The statement that changes the environment cannot be used, because the assert only takes effect in DEBUG. If you do this, you will encounter problems when the program is actually running.

Error: assert(i++ < 100)

This is because if there is an error, such as i=100 before execution, then this statement will not be executed, then the i++ command will not be executed.

correct: assert(i < 100);

         i++;


4. The assert and the following statement should be a blank line to form a logical and visual sense of consistency


5. In some places, assert cannot replace conditional filtering

    ASSERT is only valid in the Debug version, and is ignored if compiled for the Release version . (In C, ASSERT is a macro rather than a function), using ASSERT "assertions" is easy to output program errors during debugging.

   The function of assert() is similar. It is a function specified in the ANSI C standard. An important difference between it and ASSERT is that it can be used in the Release version.

   The disadvantage of using assert is that frequent calls will greatly affect the performance of the program and add extra overhead .

   After debugging is over, assert calls can be disabled by inserting #define NDEBUG before the statement that includes #include <assert.h>. The sample code is as follows:

#include <stdio.h>
#define NDEBUG
#include <assert.h>


When to use assertions

1. Assertions can be placed where the program is not expected to reach under normal circumstances: ASSERT( FALSE );
2. Assertions can be used to check parameters passed to private methods. (For public methods, because they are provided to external interfaces, there must be corresponding parameter checks in the method to ensure the robustness of the code)
3. Use assertions to test the preconditions and postconditions of method execution
4. Use assertions to check The immutable state of a class ensures that in any case, the state of a variable must be satisfied. (such as the age attribute should be greater than 0 and less than a suitable value)


where do not use assertions

Assertion statements are not always executed, they can be masked or enabled

therefore:

1. Do not use assertions as parameter checks for public methods, the parameters of public methods must always be executed
2. Assertion statements must not have any boundary effects, do not use assertion statements to modify variables and change the return value of the method

Guess you like

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