C Language Assert usage

Source: Internet
Author: User
Tags prototype definition

1, the Assert macro prototype is defined in <assert.h>, its role is to terminate the program execution if its condition returns an error, prototype definition:
#include <assert.h>
void assert (int expression);

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

2. The disadvantage of using assert is that frequent calls can greatly affect the performance of the program and add additional overhead. After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include <assert.h>, as shown in the following example code:
#include <stdio.h>
#define Ndebug
#include <assert.h>

3. Some suggestions:

3.1. Use assertions to capture illegal situations that should not occur. Do not confuse the difference between the illegal situation and the wrong situation, the latter being inevitable and must be dealt with.
3.2, at the entrance of the function, use assertions to check the validity of the parameters (legality).
3.3. When writing a function, repeat the examination, and ask yourself: "What assumptions do I intend to make?" Once the assumptions have been determined, the assumptions are checked using assertions.
3.4. The general textbook encourages programmers to make error-proof designs, but keep in mind that this style of programming may conceal errors. If the "impossible" thing does happen when the error-proof design is in progress, use an assertion to alarm.

4. An example:

char * clone_string (const char * source)

{

char * result;

ASSERT (Source = NULL);

result = (char *) malloc (strlen (source) + 1);

if (Result! = NULL)

{

strcpy (result, source);

ASSERT (strcmp (result, source) = = 0);

}

return result;

}

Notice that my null for source is checked with assert, but whether the result is null is judged by the IF statement, because the source must not be null if the calling code is correct, if the assertion fails, the error in the calling code needs to be modified , but result is not necessarily the return value of malloc, and it is possible to return null--if the malloc code is correct when the memory block is insufficient. Finally, the results of the strcpy are checked with an assert, because as long as the code is correct, no matter what the situation strcpy should be copied normally, it does not have the exception of malloc that exists.

C Language Assert usage

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.