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 if the appropriate resource handle cannot be selected in C++

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

Share

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

This article mainly explains "what to do if you can't choose an appropriate resource handle in C++". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn what to do if you can't choose the appropriate resource handle in C++.

E.19: if the appropriate resource handle cannot be selected, use final_action to represent the cleanup process

Reason (reason)

Compared with try/catch, finally is more concise and less error-prone.

Example (sample)

Void f (int n)

{

Void* p = malloc (n)

Auto _ = finally ([p] {free (p);})

/ /...

}

Note (Note)

Finally is not as messy as try/catch, but it is still a special solution to specific problems. It is better to manage objects with appropriate resources. Think of finally as a last resort.

Note (Note)

Use finally as a systematic and rational way to replace goto exit in existing code. This technique can be used to deal with the problem that resources are not systematically managed.

Enforcement (implementation recommendations)

Heuristic: goto exit detected

About finally

Finally is a support function provided by gsl that can generate a cleanup action for the user to release resources.

At this point, I believe that "C++ can not choose the appropriate resource handle what to do" have a deeper understanding, you might as well to practice it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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