I always thought that assert is just an error function, in fact, it is actually a macro, and the role is not "error."
After a certain understanding of its role and use of a certain understanding of the use of assert () is like a "contractual programming", in my understanding, the meaning is that the program under my assumptions, can operate normally good, in fact, the equivalent of an IF statement:
if (if established)
{
Normal operation of the program;
}
Else
{
Error && Termination procedure! (Avoid larger errors due to program run)
}
But in this case, there will be countless if statements, even if the parentheses of an if statement go from the file header to the end of the file, and in most cases we have to validate the hypothesis that it is just an accidental event, or if we just want to test that some of the worst cases happen, so here is the assert ( ).
The prototype of an Assert macro is defined in assert.h, and its function is to terminate the program execution if its condition returns an error.
2 void assert (int expression);
The function of an assert is to evaluate expression expressions, if the value is False (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.
The disadvantage of using assert is that frequent calls can greatly affect the performance of the program and add additional overhead.
After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include, as shown in the following example code:
3 #include
Usage Summary and Precautions:
1) Verify the legitimacy of incoming parameters at the beginning of the function
Such as:
9
2) Each assert examines only one condition, because when multiple conditions are checked, if the assertion fails, it is not possible to visually determine which condition failed
Bad: assert (noffset>=0 && noffset+nsize<=m_ninfomationsize);
Good: assert (noffset >= 0) ;
assert (noffset+nsize <= m_ninfomationsize);
3) cannot use statements that alter the environment. Because the assert only takes effect in debug, if you do, you will use the program to run into a problem
Error: assert (i++ < 100)
This is because if an error occurs, such as i=100 before execution, then this statement will not be executed, then i++ This command is not executed.
Correct: assert (i < 100)
i++;
4) The Assert and the following statements should be empty lines to form a logical and visual sense of consistency
5) in some places, assert is not a substitute for conditional filtering
programs are generally divided into debug& nbsp; version and release version,debug versions are used for internal debugging,release release to users. Assertion assert is a macro that works only in the debug version, and it is used to check for situations where "should not" occur. The following is a memory copy program that, during the run, if the parameters of the assert are false, then the program will be aborted (there will generally be a prompt dialog stating where the Assert was raised).
here are a few principles for using assertions :
(1) Use assertions to catch illegal situations that should not occur. Do not confuse the difference between the illegal situation and the wrong situation, the latter being inevitable and must be dealt with.
(2) Use assertions to confirm the parameters of the function.
(3) When writing a function, repeat the examination, and ask yourself: "What assumptions do I intend to make?" Once the assumptions have been determined, the assumptions are checked using assertions.
(4) General textbooks encourage programmers to design for error-proof, but keep in mind that this style of programming conceals errors. When error-proof programming occurs, an assertion is used to alert you if the "impossible" thing does happen.
ASSERT () is a macro that is used frequently when a debugger is run, and it calculates the expression in parentheses when it runs, and if the expression is False (0), the program reports an error and terminates execution. If the expression is not 0, the following statement continues. This macro usually turns out that there is clearly illegal data in the program, and if a termination procedure is used to avoid serious consequences, it is also easy to find errors.
Assert is only valid in debug versions and is ignored if compiled to release version.
Usage of assertion (assert)
I always thought that assert is just an error function, in fact, it is actually a macro, and the role is not "error."
After a certain understanding of its role and use of a certain understanding of the use of assert () is like a "contractual programming", in my understanding, the meaning is that the program under my assumptions, can operate normally good, in fact, the equivalent of an IF statement:
if (if established)
{
Normal operation of the program;
}
Else
{
Error && Termination procedure! (Avoid larger errors due to program run)
}
But in this case, there will be countless if statements, even if the parentheses of an if statement go from the file header to the end of the file, and in most cases we have to validate the hypothesis that it is just an accidental event, or if we just want to test that some of the worst cases happen, so here is the assert ( ).
The prototype of an Assert macro is defined in assert.h, and its function is to terminate the program execution if its condition returns an error.
1 " assert.h "
2 void int expression);
The function of an assert is to evaluate expression expressions, if the value is False (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.
the disadvantage of using assert is that frequent calls can greatly affect the performance of the program and add additional overhead.
After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include, as shown in the following example code:
1
2 #define
3 #include
Usage Summary and precautions:
1) Verify the legitimacy of incoming parameters at the beginning of the function
Such as:
1 intResetbuffersize (intNnewsize)
2{
3 //function: Change buffer size,
4 //parameter: Nnewsize buffer new length
5 //return value: Buffer current length
6 //Note: Keep the original information content unchanged nnewsize<=0 means clear buffer
7ASSERT (Nnewsize >=0);
8ASSERT (Nnewsize <= max_buffer_size);
9
Ten...
One}
2) Each assert examines only one condition, because when multiple conditions are checked, if the assertion fails, it is not possible to visually determine which condition failed
bad: Assert (noffset>=0 && noffset+nsize<=m_ninfomationsize);
good: Assert (noffset >= 0);
ASSERT (Noffset+nsize <= m_ninfomationsize);
3) You cannot use a statement that alters the environment, because assert only takes effect in debug, and if you do, you will use the program to run into problems when it is actually running
Error: Assert (i++ <)
This is because if there is an error, such as i=100 before execution, then this statement will not be executed, then i++ This command will not be executed.
correct: Assert (I <)
i++;
4) Assert and subsequent statements should be empty lines to create a logical and visual sense of consistency
5) In some places, assert cannot replace conditional filtering
The program is generally divided into debug version and release version,debug version for internal debugging,release release to users. Assertion assert is a macro that works only in the debug version, and it is used to check for situations where "should not" occur. The following is a memory copy program that, during the run, if the parameters of the assert are false, then the program will be aborted (there will generally be a prompt dialog stating where the Assert was raised).
here are a few principles for using assertions :
(1) Use assertions to catch illegal situations that should not occur. Do not confuse the difference between the illegal situation and the wrong situation, the latter being inevitable and must be dealt with.
(2) Use assertions to confirm the parameters of the function.
(3) When writing a function, repeat the examination, and ask yourself: "What assumptions do I intend to make?" Once the assumptions have been determined , the assumptions are checked using assertions.
(4) General textbooks encourage programmers to design for error-proof, but keep in mind that this style of programming conceals errors. When error-proof programming occurs, an assertion is used to alert you if the "impossible" thing does happen.
ASSERT () is a macro that is used frequently when a debugger is run, and it calculates the expression in parentheses when it runs, and if the expression is False (0), the program reports an error and terminates execution. If the expression is not 0, the following statement continues. This macro usually turns out that there is clearly illegal data in the program, and if a termination procedure is used to avoid serious consequences, it is also easy to find errors.
Assert is only valid in debug versions and is ignored if compiled to release version.
Assert the use of assert