The difference between assert and if

Source: Internet
Author: User

assert or if

At the beginning of the code, when the program check the effectiveness of the application sometimes used to assert, sometimes with if, feel very confused. For example, after most of the malloc functions, the manipulation of the pointer is checked with an assert, which may create an illusion that the detection of the application space after malloc is done with assert, but may also see the use of if to determine and process malloc, Is it an if or an assert?

Here's a definition of assert in the library, and I've removed some other places:

#ifdef Ndebug

#define ASSERT (_expression) ((void) 0)

#else

#define ASSERT (_expression) (void) (!! (_expression)) || (_wassert (_crt_wide (#_Expression), _crt_wide (__file__), __line__), 0))

#endif

Above is the definition of assert in the library, Ndebug is non-debug mode, that is, release mode. As you can see, the Assert macro in release mode has nothing to do, but in debug mode, if the value of the expression is 0, the message is output and the execution of the program is terminated. The expression is true without any action, so the assertion fails, indicating that there is a bug in the program and that there is a situation where it should not be expected to occur. That is, in release mode, the Assert macro is equivalent to nonexistent.

Assert is used to find errors in the runtime , and the errors found are related to the implementation of the program. The most fundamental benefit of using assertions is the automatic discovery of many run-time errors that can be found at the birthplace of the error, so that the programmer quickly finds and handles the error.

assert is generally used to check the legality of function parameters (validity) rather than correctness, but a legitimate program is not necessarily the correct one. parameters are null or not initialized, and these are illegal situations that cause programs to not function properly. The purpose of using Assert is to catch illegal situations that should not occur at run time. do not confuse the difference between the illegal situation and the error condition, the former is the situation that the programmer does not want to see that can cause the program not to run normally, the latter is the case that the program runs naturally and must take the initiative to make the process. For example, for a mill flour machine, there is an abnormality did not grind the flour, the first case is the grinding machine due to the problem of the motor, so that the power is not off the flour; Another situation is that we are loading the raw material is corn instead of wheat, so we can not get flour. Of course, the first situation is that nobody wants to see it. Once it happens it causes the flour machine to be abnormal (terminated), and in the second case we do something that we can handle.

Example 1:

BOOL Fun (ptype* p)

{

ASSERT (P);

......

}

Having said so much, is it an if or an assert for the above example? Try to analyze: in class we have said that the pointer is not under what circumstances all need to determine whether null, this depends on the specific scenario , such as a pointer to the first effective node of the list, if the list is not a node in the table when the pointer points to NULL,  This is a legitimate situation, so it is not possible to assert using Assert. When you are sure that the pointer is null for an illegal situation, it should not have occurred in your program, and if so, it would indicate that there is a bug in your program somewhere. That is to say, in the process of calling the function, there are some errors in the program logic, which is asserted with assert, then it will be very unhappy, angry interrupt program execution flow, pointing to the nose to tell you the null pointer problem, you need to catch the worm (ie, debug), So the assert is convenient for us to debug the location to troubleshoot problems, and if you use if, the program may continue to execute, do not throw the error, this bug is hidden, perhaps one day will erupt, the serious outbreak of which may be a disaster, if this bug hidden deep, It's pretty hard for us to get it out.

Example 2;

void Fun ()

{

int* p = (int*) malloc (sizeof (int));

ASSERT ( p);

}

here is a false usage of the assertion, p is a pointer to request memory, there is the possibility of application failure to request memory, then malloc will return NULL, this situation exists, not in the process of running the illegal situation, so it is best to use if to judge. Sometimes, for defensive programming, you might see this:void Fun ()

{

int* p = (int*) malloc (sizeof (int));

if (NULL = = p)

ASSERT (P);

}

Summarize:

1. Assert is a debug macro and not a function, only valid at Debug.

2, use the Assert to catch the program in the process of the illegal situation , in your program, if a certain situation will not appear, if it appears, it means that your program in a block error, it is best to use assert assertion, such as division when the divisor is not 0 , and the situation may appear and be legal, it is best to use if to judge, such as malloc space.


This article is from the "10911544" blog, please be sure to keep this source http://10921544.blog.51cto.com/10911544/1773317

The difference between assert and if

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.