Usage of assert

Source: Internet
Author: User

When assert is used for debugging, determine whether a statement is true.

Assert is a macro, not a function. In the assert.h header file of C.

The function of an assert is to evaluate the expression first, if its value is false (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.

The disadvantage of using assert is that frequent calls can greatly affect the performance of the program and add additional overhead.

1. Verify the legality of the parameters at the beginning of the function

2. Each assert examines only one condition, because when multiple conditions are checked, if the assertion fails, it is not possible to visually determine which condition failed

3. You cannot use a statement that alters the environment, because assert only takes effect in debug, and if you do, you will use the program to run into a problem error: assert (i++ < 100)

This is because if there is an error, such as i=100 before execution, then this statement will not be executed, then i++ This command will not be executed.  Correct: Assert (i < 100); i++;

Resources:

http://blog.csdn.net/setone/article/details/5573036

Usage of assert

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.