C++ assertion makes the program have a temper

1. Background

Assertions are rarely used, and there may not be serious problems.
According to its meaning, it is very necessary, such as the judgment of being empty; it makes the program have a temper, if it doesn’t work, just quit it. In fact, only in this way can it be guaranteed Yes, the program does not make mistakes. If an error occurs and the program is still running, a bigger error will occur. At this time, the program directly chooses to lose its temper and quit, which is the best choice. This is the meaning of assertion

1.1 The specific usage is as follows:

assert, translated into Chinese as assertion, note that it is a macro definition, not a function.
In c++, to use assert, you can include the cassert header file, and cassert is finally referenced assert.h.
The function of assert is the same as in other languages. If the condition in it returns an error, the code will terminate and output the source file, error code, and line number.

1.2 Precautions when using

In programming, its role is to use assertions to prevent impossible things, which is the meaning of assertions.
Whenever you catch yourself thinking "of course this can't happen", add code to check for this.
Do not use assertions in place of real processing. Assertions check for things that cannot happen, such as the parameter cannot be NULL, or it can be understood that this must be the case.

assert is for avoiding obvious mistakes, not for handling exceptions. Errors and exceptions are not the same, errors should not occur, exceptions are inevitable. C language exceptions can be handled through conditional judgment, and other languages ​​have their own exception handling mechanisms.

A very simple rule of using assert is that it is used at the beginning of a method or function. If it is used in the middle of a method, it needs to be carefully considered whether it should be used. At the beginning of the method, a functional process has not been started, and problems that occur during the execution of a functional process are almost all abnormal.
After an error occurs, the program cannot continue to run, and there will be greater problems if it continues to run, and the program must be terminated
Abnormal, that is, an error that may be handled, the program can continue to run

2 instances

 1 #include <cstdio>
  2 #include <cassert>
  3 #include <string.h>
  4 
  5 void func1(char *cName) {
    
    
  6    assert(cName != NULL);
  7    strcpy(cName,"liming");
  8 }  
  9 
 10 int main(int argc, char const *argv[])
 11 {
    
    
 12     char *cName = NULL;
 13     func1(cName);
 14     printf("cName=%s", cName);
 15     
 16     return 0;
 17 }   
 18 

Operation status:

root@mkx:~/learn/assert1# ./assert1 
assert1: assert1.cpp:6: void func1(char*): Assertion `cName != NULL' failed.
已放弃 (核心已转储)
root@mkx:~/learn/assert1# 

3 Summary

image.png

To put it simply, there was an unbearable mistake, and I had no choice but to collapse. I have a temper.
From this, it can be seen that it is right for people to have a temper. Temper is a kind of protection for people, and the procedure is the same. .

Guess you like

Origin blog.csdn.net/maokexu123/article/details/126549023