Introduction to assert () Functions

Source: Internet
Author: User
Tags prototype definition

Assert can be in the following two forms:
Assert expression1;
Assert expression1: expression2;
Expression1 should always generate a Boolean value.
Expression2 can be any expression that generates a value. This value is used to generate a string message that displays more debugging information.
Assertions are disabled by default.

The assert macro prototype is defined in <assert. h>,
Its function is to terminate program execution if its condition returns an error.
Prototype definition:
# Include <assert. h>
Void assert (INT expression );
The role of assert is to calculate the expression. If its value is false (that is, 0), It prints an error message to stderr and then calls abort to terminate the program running.

See the following program list:
# 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 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 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 tests only one condition, because when multiple conditions are verified at the same time, if the assert fails, you cannot intuitively determine which condition fails.
Bad: assert (noffset> = 0 & noffset + nsize <= m_ninfomationsize );
Good: assert (noffset> = 0 );
Assert (noffset + nsize <= m_ninfomationsize );

3)The statement to change the environment cannot be used, because assert only takes effect in 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 a line to form a logical and visual sense of 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.