C language assert function - C language zero foundation tutorial

Posted by NiGHTFiRE on Mon, 08 Nov 2021 22:39:24 +0100

catalogue

Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language

1, Introduction to assert function

Assertions are familiar to everyone, and most programming languages also have the feature of assertions. Simply put, an assertion is a check on a hypothetical condition. assert macro prototype definition:

//Need to include header file < assert. H >
#include <assert.h>

/*
 Description: terminates program execution if its condition returns an error

 Parameters:
    expression : Conditional judgment or expression
 */

void assert( int expression );

assert The value of the expression will be checked to determine whether the executor needs to be terminated. That is, if the value of expression is false (i.e. 0), it will first print an error message to the standard error stream stderr, and then call abort Function to terminate program operation; Otherwise, assert has no effect.

2, The assert function uses

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - C language assert function
//@Time:2021/07/18 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
int main( void )
{
    FILE *fp;

    fp = fopen( "456.txt", "w" );//Open a file in a writable manner. If it does not exist, create a file with the same name
    assert( fp );                           //So there will be no mistakes here
    fclose( fp );

    fp = fopen( "123.txt", "r" );//Open a file as read-only. If it does not exist, opening the file fails
    assert( fp );                           //So there's a mistake here
    fclose( fp );                           //The program will never run here
    return 0;
}

By default, assert Macros work only in the Debug version (internal Debug version) and will be ignored in the Release version (Release version).

3, Usage Summary and precautions of assert function

1. Use assert to verify the validity of the passed parameters at the beginning of the function

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - C language assert function
//@Time:2021/07/18 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


int resetBufferSize(int nNewSize)
{
  //Function: change buffer size,
  //Parameter: nNewSize buffer length
  //Return value: current length of buffer
  //Note: keep the original information content unchanged. Nnewsize < = 0 means to clear the buffer
  assert(nNewSize >= 0);
  assert(nNewSize <= MAX_BUFFER_SIZE);
  ...
}

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

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - C language assert function
//@Time:2021/07/18 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


//Wrong writing
assert(a>=0 && b<=0 && c>0);

//Correct writing
assert(a>=0);
assert(b<=0);
assert(c>0);

3. assert The disadvantage is that frequent calls will greatly affect the performance of the program and increase additional overhead.

4, Guess you like it

  1. Difference between C language array subscript out of bounds and memory overflow
  2. C language uses pointers to traverse arrays
  3. Difference between C language pointer and array
  4. Difference between C language pointer array and array pointer
  5. C language field pointer
  6. C language function value transfer and address transfer
  7. C language function indefinite length parameter
  8. C language function pointer
  9. C language pointer function
  10. C language callback function callback
  11. C language #pragma once
  12. Difference between C language #include < > and #include ""
  13. C language const modifier function parameters
  14. Difference between const and define in C language
  15. C language # operators
  16. C language ## operators
  17. C language__ VA_ARGS__
  18. C language##__ VA_ARGS__
  19. C language function indefinite length parameter##__ VA_ARGS__ Classic case
  20. C language va_start macro
  21. C language va_end macro
  22. C language va_arg macro
  23. C language vprintf function
  24. C language va_start / va_end / va_arg custom printf function
  25. C language main function
  26. C language main function parameter main(int argc, char *argv [])
  27. C language exit function
  28. C language abort function
  29. C language assert function

No reprint without permission: Ape programming » C language assert function

This article is written by blog - Ape programming Ape programming release!

Topics: C