Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the usage and points for attention of assert

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the usage of assert and matters needing attention". In daily operation, I believe many people have doubts about the usage of assert and matters needing attention. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about the usage of assert and matters needing attention. Next, please follow the editor to study!

The function of assert is to evaluate the expression expression now. If its value is false (that is, 0), it first prints an error message to stderr, and then terminates the program by calling abort.

I always thought that assert is just an error function, in fact, it is actually a macro, and the function is not "error report".

After a certain understanding of it, I have a certain understanding of its function and usage. The use of assert () is like a kind of "contract programming". In my understanding, it means that the program can work properly under my assumptions, which is actually equivalent to an if statement:

If (assuming) {the program is running properly;} else {error & & terminating the program! (avoid bigger errors caused by the running of the program)}

But if you write like this, there will be countless if statements, or even appear, a parenthesis of an if statement from the beginning of the file to the end of the file, and in most cases, the hypothesis that we want to verify is just an accidental event, or we just want to test whether some worst-case scenario happens, so here we have assert ().

The prototype of the assert macro is defined in assert.h, and its purpose is to terminate program execution if its condition returns an error.

# include "assert.h" void assert (int expression)

The function of assert is to evaluate the expression expression now. If its value is false (that is, 0), it first prints an error message to stderr, and then terminates the program by calling abort.

The disadvantage of using assert is that frequent calls will greatly affect the performance of the program and add additional overhead.

After debugging, you can disable the assert call by inserting # define NDEBUG before the statement that contains # include, as shown in the sample code:

# include # define NDEBUG # include

Summary of usage and points for attention

1) verify the validity of the passed parameters at the beginning of the function

Such as:

Int resetBufferSize (int nNewSize) {/ / function: change buffer size, / / Parameter: new length of nNewSize buffer / / return value: current length of buffer / / description: keep the original information content unchanged nNewSize= 0); assert (nNewSize= 0 & & nOffset+nSize= 0); assert (nOffset+nSize

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report