Assert assert usage Summary

Source: Internet
Author: User
Tags prototype definition

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

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

Assert is used to calculate the expression. If its value is false (that is, 0), 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;
}

[Root @ localhost error_process] # GCC badptr. c
[Root @ localhost error_process] #./A. Out
A. Out: badptr. C: 14: Main: assertion 'fp' failed.
Abandoned

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 statement containing # include <assert. h>. For example:CodeAs follows:
# Include <stdio. h>
# Define ndebug
# Include <assert. h>

Usage summary and precautions:
1) Check the validity of input parameters at the beginning of the Function
For example:

Int resetbuffersize (INT nnewsize)
{
// Function: Change the buffer size,
// Parameter: New length of the nnewsize Buffer
// Return value: Current Buffer Length
// Note: keep the original information unchanged. nnewsize <= 0 indicates clearing the buffer.
Assert (nnewsize> = 0 );
Assert (nnewsize <= max_buffer_size );

...
}

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.

Poor: assert (noffset> = 0 & noffset + nsize <= m_ninfomationsize );

Good: assert (noffset> = 0 );
Assert (noffset + nsize <= m_ninfomationsize );

3) You cannot use a statement to 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.

5) In some cases, assert cannot replace conditional filtering.

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.