I saw it when I was writing code today idea There's one assert Tips for , Driven by curiosity, explore it , Follow one by one


Assert (assert) Meaning of

assert() The usage of is like a " Contractual programming ", In my understanding , What it means is , The program is under my assumptions , Can operate normally and well .

Actually, it's equivalent to one if sentence : 
if( Hypothesis established ) { The program runs normally ; } else { report errors && Termination procedure !( Avoid larger errors caused by program operation ) }
But if it's written like this , There will be countless if sentence , Even appear , One if
The parentheses of the statement run from the beginning of the file to the end of the file . And in most cases , The assumptions we want to test , It's just an accident , Or do we just want to test it , Did some of the worst happen , So here we have


assert The function of is to evaluate the expression expression , If its value is false ( mean 0), Then it goes first stderr Print an error message , Then by calling abort
To terminate the program .


use assert The disadvantage is , Frequent calls will greatly affect the performance of the program , Add extra overhead .

Usage Summary

Basic application

Verify the validity of the passed in parameter at the beginning of the function
int resetBufferSize(int nNewSize) { // function : Change buffer size , // parameter :nNewSi