Use the assertion in VC ++

xiaoxiao2021-03-06  114

Use the assertion in VC

1 ??????? the expression of the assertion

1.1 ????? grammar

As a macro, the format is: Assert (logical expression booleaneXpression)

A logical expression can be any expression, its value is 0 or non-0.

From the perspective of code readability, this expression should be not included in the logic comparison operation with the Boolean variable.

1.2 ?????

Assessment only in the DEBUG process.

When the logical expression has false (0), the compiler will automatically stop the program's operation and give relevant program diagnostic information.

For the Release version of the code, the Assert Macro will not be processed, so it does not run any blocking effect on the normal run. For MFC, if you want to have a similar function in the Release version, you can use the Verify macro. This macro is similar to the assertion.

1.3 ????? diagnostic information

Assertion Failed in File

in line

The above is the format of the diagnostic information. Where file name represents the file where the error program code is located, Line Number is the assertion of the problem.

2 ??????? assertion method

2.1 ????? function call correctness check

You can check the rationality of the function parameters.

In actual work, some functions require input parameters to meet some assumptions, and these constraints are not available. For example, the following code, to see some parameters of a right-angled triangle according to three edges, you must confirm that these three edges are reasonable.

Type getValue (int A, int B, int C)

{

??? assert ((a <= c) && (b <= c)) // specify the third parameter as the largest

??? assert ((A * a b * b) == C * c)

??? // to do something

.........

// end function

}

Here, assertion The third parameter must be the largest edge length, then further requires the square of the two short sides and must be equal to the square of the long side.

Similarly, we can use the pointer given by the hierarchy to be valid.

......

AskERT (NULL! = Poutbuffer)

...

There are two benefits of the rationality of the detection parameters in the function:

1 Assume the robustness of the function itself;

2 Make problems easy to position.

The parameters given by the main debut may sometimes be inexplicably, use assertions, and can find many difficult logical errors.

2.2 ????? Check memory allocation

In MFC, since Windows virtual memory, Release is not possible to deplete memory after virtual memory. Therefore, if the allocation of the memory is not comparable, it is best to use the assertion if it is used to check the memory.

E.g:

......

PBUF = (char *) Malloc (sizeof (char) * 100);

Assert (null! = Pbuf)

......

This neither reduces code execution efficiency and checks if allocated memory is successful.

2.3 ????? Prevention and treatment of wild pointers

Everyone knows how to initialize when defining a pointer:

INT * PCOUNT = NULL;

Then allocate space,

Pcount = new int [10];

PROCESS ...

After the release space:

delete [] PCOUNT;

At this time, where is PCOUNT? Or the original address, but the address has been released by the system, so this pointer turns into "wild pointer", if used, inevitably cause errors. Here, it should be used to ensure that the NULL value should be assigned. Pcount = NULL;

? ...

? Assert (null == pcount); // if You'Ve Forgotten Delete, Here Will Output An Alart.

? Keep in mind that after the release space is released, make sure that this pointer has been assigned NULL to avoid a wild pointer.

2.4 ????? msdn example

// eXample for assert

CAGE * PCAGE = New Cage [21]; // Cage is Derived from COBject.

AskERT (PCAGE! = NULL)

Assert (pcage-> iskindof (runtime_class (CAGE)))

// Terminates Program Only if pcage is not a cage *.

The above examples of the examples are used in the MSDN, where the object to which PCAGE points to be used is a valid CAGE class.

3 ??????? payment

1 Assessing the case where it is illegal, not an error. Don't try to trouble with the assertion.

2 Once the function has any assumptions to the parameters, be sure to use assertions at the entry of the function to confirm whether the assumption is implemented.

3 Give each assert to the comment to make the assertion to the wrong mistake.

?

Reference

1 Steve Maguire, Writing Clean Code.

2 Lin Rui, High Quality C / C Programming Guide

3 Microsoft, MSDN Document

?

V1.0 2003/9/9

转载请注明原文地址:https://www.9cbs.com/read-125464.html

New Post(0)