C ++ assert () Assertions

Source: Internet
Author: User

Assert is a macro definition. The prototype is defined in <assert. h>:

# Include <assert. h>
Void assert (int expression );


The function is to terminate the program execution if the conditional expression returns an error.

The disadvantage of using assert 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:
# Include <stdio. h>
# Define NDEBUG
# Include <assert. h>


Usage summary and precautions:


1) Check the validity (validity) of parameters using assertions at the function entrance ).
2) Each assert only tests one condition, because when multiple conditions are verified simultaneously, if the assert fails, it is impossible to intuitively determine which condition fails.
3) You cannot use statements that change the environment, because assert takes effect only on DEBUG. If so, problems will occur when the program is actually running.
Error: assert (I ++ <100)
This is because if an error occurs, for example, I = 100 before execution, this statement will not be executed, and the I ++ command will not be executed.
Correct: assert (I <100)
I ++;

4) assert and the following statement should be empty for logical and visual consistency.


 

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.