Analysis of C ++ assert () function Application Methods

Source: Internet
Author: User
Tags prototype definition

As an experienced programmer, the C ++ programming language should not be unfamiliar. Its application can help us easily implement various functional requirements. Here we will give a detailed introduction to some basic applications of the C ++ assert () function.

The assert macro prototype is defined in <assert. h>. Its function is to terminate program execution if its condition returns an error. The prototype definition is as follows:

 
 
  1. #include < assert.h>   
  2. void assert( int expression );  

The C ++ assert () function is used to calculate the expression. If its value is false, it is 0. Then it prints an error message to stderr first, then, terminate the program by calling abort. See the following program list badptr. c:

 
 
  1. # Include <stdio. h>
  2. # Include <assert. h>
  3. # Include <stdlib. h>
  4. Int main (void)
  5. {
  6. FILE * fp;
  7. Fp = fopen ("test.txt", "w ");
    // Open a file in writable mode. If the file does not exist, create a file with the same name.
  8. Assert (fp); // so there will be no error
  9. Fclose (fp );
  10. Fp = fopen ("noexitfile.txt", "r ");
    // Open a file in read-only mode. If the file does not exist, it fails to be opened.
  11. Assert (fp); // an error occurs here
  12. Fclose (fp); // the program will never be executed here
  13. Return 0;
  14. }
  15. [Root @ localhost error_process] # gcc badptr. c
  16. [Root @ localhost error_process] #./a. out
  17. A. out: badptr. c: 14: main: Assertion 'fp' failed.

Abandoned

The disadvantage of using the C ++ assert () function is that frequent calls will greatly affect program performance and increase additional overhead. After debugging, you can disable the assert call by inserting # define NDEBUG before the # include <assert. h> statement. The sample code is as follows:

 
 
  1. #include < stdio.h>   
  2. #define NDEBUG   
  3. #include < assert.h> 

The above is an introduction to the C ++ assert () function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.