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

Why don't you leak any resources in C++?

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "Why don't divulge any resources in C++". In daily operation, I believe many people have doubts about why C++ should not leak any resources. The editor looked up all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "Why don't leak any resources in C++?" Next, please follow the editor to study!

P.8: Don't leak any resources (do not disclose any resources) Reason (reason)

Even a slow growth in resources will, over time, exhaust the availability of those resources. This is particularly important for long-running programs, but is an essential piece of responsible programming behavior.

Even if the resource grows slowly, after a certain period of time, the availability of the resource will eventually be exhausted. This is especially important for long-running programs and is an essential part of important programming activities.

Example, bad (negative example)

Void f (char* name) {FILE* input = fopen (name, "r"); / / If (something) return; / / bad: if something = = true, a file handle is leaked / /... Fclose (input);} translator's note: for some reason, when the code exits execution before fclose, the file cannot be closed.

Prefer RAII: (a better example of using RAII)

Void f (char* name) {ifstream input {name}; / / If (something) return; / / OK: no leak / /...}

Translator's note:

Resource acquisition is initialization (Resource Acquisition Is Initialization), or RAII, is a C++ programming technique that binds the life cycle of resources that must be requested before use (allocated heap memory, threads of execution, open sockets, open files, locked mutexes, disk space, database connections, etc.-anything in a restricted supply) to the lifetime of an object.

Note (Note)

Leaks are also often referred to as "anything that hasn't been cleaned up". The more important category is "anything that will not be cleaned up". For example, allocate an object from the heap and then lose the last pointer to that object. This rule should not be understood as a requirement that long life cycle objects must be released when the program stops running. For example, freeing memory processing simplifies code by using the closed files guaranteed when the process stops running, but relying on strict cleanup rules can be simpler and more secure.

Note (Note)

The implementation of life cycle safety rule groups can eliminate resource leakage. When used in conjunction with the resource security mechanism provided by RAII, the need for "garbage collection" can be excluded (by not generating garbage). Combined with execution types and boundary rule groups, you can get complete type and resource security, everything is guaranteed by the tool.

Enforcement (implementation of recommendations)

Look at pointers: Classify them into non-owners (the default) and owners. Where feasible, replace owners with standard-library resource handles (as in the example above). Alternatively, mark an owner as such using owner from the GSL.

Attention pointer: classifies according to whether the owner has or not (the default is none). If possible, replace the owner with a resource handle in the standard library (such as the example above). Use the owner keyword in GSL to identify the owner.

Look for naked new and delete

Focus on direct new and delete

Attention

Look for known resource allocating functions returning raw pointers (such as fopen, malloc, and strdup)

Focus on known resource allocation functions that return the original pointer (such as fopen,malloc and strdup)

At this point, the study of "Why do not leak any resources in C++" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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