[Memory Management] Summary of smart pointers and memory pools

Source: Internet
Author: User

Summary:

Memory Management is C ++ProgramA permanent topic in development. Because there is no garbage collection mechanism, managing system resources such as memory with caution is a face-to-face problem for every C ++ developer.

The boost. smart_ptr Library provides several new smart pointers to make up for the STD: auto_ptr deficiency, which can effectively eliminate the use of new and delete display, reduce or even eliminateCodeResource leakage.

Scoped_ptr is the easiest way to learn and use it in the smart_ptr library. Its behavior is similar to that of auto_ptr, but its ownership is clear. It clearly shows that this smart pointer can only be used in declared scopes and cannot be transferred, any copy operation on it will fail. This feature is very useful for code later maintenance.

Shared_ptr is probably the most useful smart pointer. It is also the most "intelligent" of these smart pointers. It can not only manage memory, but also manage other system resources and can be applied to many occasions. It can automatically calculate the reference count of the pointer, and its behavior is closest to that of the original pointer. Shared_ptr can be used almost anywhere the original pointer can be used without the risk of resource leakage. Shared_ptr not only saves pointers, but also automatically releases resources associated with pointers by configuring the deleteer.

In addition to the basic usage, you should also understand the pimpl usage and apply it to the factory model. These usage further demonstrates its powerful functions. To facilitate the use of shared_ptr, The smart_ptr Library also provides the factory function make) shared (), further eliminating the use of the new operator in the code.

Scoped_array and shared_array are extensions of scoped_ptr and shared_ptr for dynamic arrays. They provide automatic deletion proxy for dynamic arrays. shared_array has more functions than scoped_array, however, vector and shared_ptr <vector <> should be used unless the program has very demanding performance requirements.

Weak_ptr can observe shared_ptr statically without affecting the reference count.

The pool library is another useful tool provided by the boost library in terms of memory management. It uses an efficient memory pool to manage memory resources. The pool Library provides four types of memory pools: pool, object_pool, singleteon_pool, and pool_alloc, which are suitable for applications in various situations. They can be regarded as a small garbage collection mechanism, and objects can be dynamically created in the memory pool without worrying about the collection, you do not need to make any changes to the original class.

Object_pool can manage the creation and destruction of objects in a unified manner, and can be well applied in various scale object-facing software systems. As for pool_alloc, it is a memory distributor that complies with the c ++ standard. It is fast and efficient, but generally the built-in splitters of STL work better together. When using pool_alloc, you must carefully evaluate the possible impact.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.