Assert ()

Source: Internet
Author: User
What is assert?
The header file of assert is assert. h. Usage: assert (expression ); When expression is false,ProgramIt will terminate and the expression information will pop up. This may not be clear. For example
 
Assert (length> = 0); // when the length is smaller than 0, the program is terminated, and the assersion failed: lenght> = 0 message is displayed, prompting that an error occurs. That is, to tell the programmer that length is not a non-negative number.
You can also add more error display information, as shown in figure
 
Assert (length> = 0 & "length can't possibly be negative ");
Or
 
Assert ("length can't possible be negative", length> = 0 ));
What is the use of assert? Assert can be understood as follows: I want to ensure that the expression is correct, but if the program is incorrect, stop running and tell me. 1. Assert can check the validity of parameters at the beginning of the function.
Int resetbuffersize (INT nnewsize) {// function: Change the buffer size, // parameter: New length of the nnewsize buffer // return value: current length of the buffer // description: nnewsize <= 0 indicates clearing the buffer assert (nnewsize> = 0); Assert (nnewsize <= max_buffer_size );...}
Assert () Is there any note that assert () runs in the debugging state, and its overhead is still quite large. Therefore, you can disable assert after debugging the program. This can be done in this way.
 
# Define ndebug # include <assert. h>

Add ndebug before assert. h.

Your program will eventually run in the assert closed state. Your program design will not depend on the assert () function, otherwise, the program running results will be different from what you think. For example
 
Length = 100; Assert (length ++ );

At this time, when assert () is opened and assert () is closed, the execution results of the program may be completely different, so this is a bad design.

Ru
 
Assert (FOO ());

The program running depends on foo ();

Can be changed
 
Int ret = Foo (); Assert (RET );

 

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.