Assert function Parsing

Source: Internet
Author: User

I. assert is a macro

Specifically: in C, ASSERT is a macro rather than a function.
Assert () is a macro that is often used when debugging programs. When the program is running, it calculates the expressions in parentheses.

If the expression is FALSE (0), the program reports an error and terminates the execution.

If the expression is not 0, execute the following statement.

This macro is often used to determine whether there is clearly illegal data in the program. If so, terminate the program to avoid serious consequences, and report the "location" of the error ".

I. assert is often required by the interviewer during the interview. How can this macro be implemented?

The implementation code of the assert macro is as follows:


[Cpp] # include <iostream>
# Include <stdio. h>
Using namespace std;
 
Void _ assert (const char * p, const char * f, int n)
{
Cout <p <endl;
Cout <f <endl;
Cout <n <endl;
}
Method 1:
# Define assert (exp )\
{If (! Exp) printf ("% s \ n % d \ n" ,__ FILE __, # exp ,__ LINE __);}
 
Method 2:
# Define assert (e )\
(E )? (Void) 0: _ assert (# e, _ FILE __, _ LINE __))
 

Void main ()
{
Int * p = NULL;
Assert (p! = NULL );
}

# Include <iostream>
# Include <stdio. h>
Using namespace std;

Void _ assert (const char * p, const char * f, int n)
{
Cout <p <endl;
Cout <f <endl;
Cout <n <endl;
}
Method 1:
# Define assert (exp )\
{If (! Exp) printf ("% s \ n % d \ n" ,__ FILE __, # exp ,__ LINE __);}

Method 2:
# Define assert (e )\
(E )? (Void) 0: _ assert (# e, _ FILE __, _ LINE __))

 
Void main ()
{
Int * p = NULL;
Assert (p! = NULL );
}

 


Iii. What should I pay attention to when using assert?

(1) Check the validity of input parameters at the beginning of the function. The sample code is as follows:


1 int resetBufferSize (int Size)
2 {
3 assert (Size> = 0 );
4 assert (Size <= MAX_BUFFER_SIZE );
7}
(2) Each assert checks only one condition.

Because when multiple conditions are verified at the same time. If the assertion fails, you cannot intuitively determine which condition causes the failure.

(3) You cannot use statements that change the environment. Because assert only takes effect in DEBUG, if this is done, the program will encounter problems during real operation.


Sample error:

Assert (I ++ <100 );
Analysis and exploration: for example, if I = 99 before the statement is executed, then the I ++ statement is executed and I = 100. However, if the I ++ value is still 99, the macro will lose its meaning.
Correct example:

Assert (I <100 );
I ++;
(4) assert and the following statement should be empty in order to form a logical and visual sense of consistency.

(5) ASSERT is only valid in the Debug version. If it is compiled into the Release version, it is ignored. Using ASSERT "ASSERT" can easily output program errors during debugging.

 

 

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.