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 reasons for avoiding the Goto statement ​ in C++

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about the reasons for avoiding using goto sentences in C++. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Reason (reason)

Readability, avoidance of errors. There are better control structures for humans; goto is for machine generated code.

Readability to avoid errors. There are other and better code structures available.

Exception (exception)

Breaking out of a nested loop. In that case, always jump forwards.

Jump out of a nested loop. In this case, always jump forward.

For (int I = 0; I

< imax; ++i) for (int j = 0; j < jmax; ++j) { if (a[i][j] >

Elem_max) goto finished

/ /...

}

Finished:

/ /...

Example, bad (negative example)

There is a fair amount of use of the C goto-exit idiom:

There is a considerable amount of C code that uses goto-exit idioms.

Void f ()

{

/ /...

Goto exit

/ /...

Goto exit

/ /...

Exit:

/ /... Common cleanup code...

}

This is an ad-hoc simulation of destructors. Declare your resources with handles with destructors that clean up. If for some reason you cannot handle all cleanup with destructors for the variables used, consider gsl::finally () as a cleaner and more reliable alternative to goto exit.

This is a particularly appropriate use scenario for destructors. Define a resource management class and perform cleanup actions in its destructor. If for some reason the destructor cannot be completely cleared in all cases, consider using gsl::finally as a more reliable alternative to the cleaner and goto.

Enforcement (implementation recommendations)

Flag goto. Better still flag all gotos that do not jump from a nested loop to the statement immediately after a nest of loops.

Mark the goto statement. It is best to identify all goto statements. There is only one exception: jumping from within a nested loop to the code immediately after the loop.

These are the reasons for avoiding the use of goto sentences in C++ shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report