In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of why explicitly allocated resources are immediately handed over to resource management objects in C++. Many people will encounter this dilemma in the operation of actual cases. then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
R.12: immediately hand over explicitly allocated resources to resource management objects
Reason (reason)
If you do not do so, a leak may occur when an exception occurs or when a return operation occurs.
Example, bad (negative example)
Void f (const string& name)
{
FILE* f = fopen (name, "r"); / / open the file
Vector buf (1024)
Auto _ = finally ([f] {fclose (f);}); / / remember to close the file
/ /...
}
Failure to allocate buffer (throw exception, translator's note) will cause the file handle to leak.
Example (sample)
Void f (const string& name)
{
Ifstream f {name}; / / open the file
Vector buf (1024)
/ /...
}
(managed with ifstream) the use of file handles is simple, efficient, and secure.
Enforcement (implementation recommendations)
Tag initializes pointers with explicitly allocated resources (the question is: how many cases can we identify when resources are allocated directly?)
This is the end of the content of "Why do you immediately hand over explicitly allocated resources to resource management objects in C++?" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.