C ++ assert ()

Source: Internet
Author: User
Tags prototype definition

The application method of the C ++ assert Macro will be described in this article.ArticleFor details

I believe that friends who are interested in this should be able to fully master the application skills based on the content we introduced.
As an experienced programmerProgramming LanguageThere should be no strangers. Its Applications can help us easily implement various functional requirements.

Here we will give a detailed introduction to some basic applications of the C ++ assert macro.

The assert macro prototype is defined in <assert. h>. Its function is to terminate a condition if an error is returned.ProgramRun,

Prototype definition:

# Include <assert. h> void assert (INT expression );

The function of the C ++ assert () macro is to calculate the expression. If the value is false (that 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:

# Include <stdio. h>

# Include <assert. h>

# Include <stdlib. h>

Int main (void)

{

File * FP;

Fp = fopen ("test.txt", "W"); // open a file in writable mode. If it does not exist, create a file with the same name.

Assert (FP); // so there will be no error

Fclose (FP );

Fp = fopen ("noexitfile.txt", "R"); // open a file in read-only mode. If it does not exist, the file fails to be opened.

Assert (FP); // an error occurs here

Fclose (FP); // the program will never be executed here

Return 0;

}

The disadvantage of using the C ++ assert () macro 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 statement containing # include <assert. h>,

ExampleCodeAs follows:

# Include <stdio. h>

# Define ndebug

# Include <assert. h>

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/acdnjjjdjkdckjj/archive/2010/05/24/5621118.aspx

 

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.