How to Use assert () C ++

Source: Internet
Author: User
Tags prototype definition

Programs are generally divided into debug and release versions. The debug version is used for internal debugging, And the release version is released to users.

Assert is a macro that only works in the debug version. It is used to check the situation where "no" occurs. Example 6-5 is a memory replication function. During the running process, if the assert parameter is false, the program will stop (generally, a dialog prompt will appear, indicating where the assert is triggered ).

Void * memcpy (void * pvto, const void * pvfrom, size_t size)

{

Assert (pvto! = NULL) & (pvfrom! = NULL); // Use assertions

Byte * pbto = (byte *) pvto; // prevents the pvto address from being changed

Byte * pbfrom = (byte *) pvfrom; // prevents the pvfrom address from being changed

While (size --> 0)

* Pbto ++ = * pbfrom ++;

Return pvto;

}

Example 6-5 Copy non-overlapping memory blocks

Assert is not a hasty macro. In order not to cause any difference between the debug and release versions of the program, assert should not produce any side effects. So assert is not a function, but a macro. Programmers can regard assert as a harmless testing method that can be safely used in any system status. If the program terminates at assert, it does not mean that the function containing this assert has an error, but the caller has an error. Assert can help us find the cause of the error.

There are few more frustrating things than the assertions that trace the program, but do not know the role of the assertions. You have made a lot of time, not to exclude errors, but to find out what the error is. Sometimes, programmers occasionally design wrong assertions. Therefore, if you do not know what the assertions check, it is difficult to determine whether the errors appear in the program or in the assertions. Fortunately, this problem is well solved by adding clear notes. This is obvious, but few programmers do this. This is like a person in the forest, seeing a "dangerous" big sign on the tree. But what is the danger? Will the tree fall? Is there a waste well? Is there a beast? Unless you tell people what danger is, this warning board cannot play a positive and effective role. Incomprehensible assertions are often ignored or even deleted by programmers.

[Rule 6-5-1] Use assertions to capture illegal situations that should not occur. Do not confuse the differences between illegal and wrong situations. The latter must exist and be handled.

[Rule 6-5-2] Check the validity (validity) of parameters using assertions at the function entrance ).

[6-5-1] When writing a function, you should repeat it and ask yourself: "What assumptions do I plan to make ?" Once the assumptions are determined, we need to use assertions to check the assumptions.

[6-5-2] Generally, textbooks encourage programmers to design error prevention, but remember that such a programming style may conceal errors. When an error prevention design occurs, if the "impossible to happen" event does occur, Use assertions to trigger an alarm.

 

The prototype of the ssert macro is defined in <assert. h>. Its function is to terminate program execution if its condition returns an error. The prototype definition is as follows:

# 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;

}

Related Article

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.