2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Smart Pointer is a tool in C++ used to automatically manage dynamically allocated memory.
It is a class that encapsulates the functionality of a raw pointer and automatically handles resource allocation and release, helping programmers avoid common memory management errors such as memory leaks, dangling pointers, and other issues.
Smart pointers work through the Resource Acquisition Is Initialization (RAII) mechanism, which means that smart pointers acquire resources when they are constructed and release them when they are destroyed. This ensures that resources are properly released even in abnormal situations.
(1) Memory leak: Memory is released manually, and can be released automatically using smart pointers.
(2) Propagation and release of shared ownership pointers, such as the destruction problem when multiple threads use the same object.
The C++ standard library provides several types of smart pointers, including:
std::unique_ptr
:
unique_ptr
An instance has exclusive ownership of the object it points to.unique_ptr
When it goes out of scope or is explicitly reset, it automatically calls its destructor and releases the managed object.std::shared_ptr
:
shared_ptr
Instances share ownership of the same object.shared_ptr
quantity.shared_ptr
When the instance goes out of scope or is reset, the reference count is reduced to zero, the object's destructor is called, and the object is released.std::weak_ptr
:
shared_ptr
The circular reference problem.shared_ptr
Manages objects but does not participate in their life cycle management.shared_ptr
When an instance points to an object,weak_ptr
It is possible to detect that an object has been destroyed.The design and use of smart pointers greatly simplifies memory management in C++ and improves the robustness and security of programs. In modern C++ programming, it is strongly recommended to use smart pointers instead of naked pointers to improve the quality and maintainability of code.
Smart pointers are a means of automatically managing dynamically allocated memory in C++ to prevent memory leaks and other resource management related problems.
std::unique_ptr
Examplestd::unique_ptr
Is a smart pointer with exclusive ownership, which ensures that only onestd::unique_ptr
The instance has control over a given resource.std::unique_ptr
When it goes out of scope or is reset, the resources it owns are automatically released.
- #include <memory>
-
- class MyClass {
- public:
- MyClass() { std::cout << "MyClass constructor called.n"; }
- ~MyClass() { std::cout << "MyClass destructor called.n"; }
- void doSomething() { std::cout << "Doing something...n"; }
- };
-
- int main() {
- // 创建一个unique_ptr来管理MyClass的实例
- std::unique_ptr<MyClass> uptr(new MyClass());
-
- // 调用doSomething方法
- uptr->doSomething();
-
- // unique_ptr会在离开作用域时自动调用MyClass的析构函数
- return 0;
- }
std::shared_ptr
Examplestd::shared_ptr
is a shared-ownership smart pointer that allows multiplestd::shared_ptr
Instances share ownership of the same resources.std::shared_ptr
Reference counting is used to track how many smart pointers point to the same resource. When the reference count reaches 0, the resource is released.
- #include <memory>
-
- class MyClass {
- public:
- MyClass() { std::cout << "MyClass constructor called.n"; }
- ~MyClass() { std::cout << "MyClass destructor called.n"; }
- void doSomething() { std::cout << "Doing something...n"; }
- };
-
- int main() {
- // 创建一个shared_ptr来管理MyClass的实例
- std::shared_ptr<MyClass> sptr(new MyClass());
-
- // 创建第二个shared_ptr,共享同一个MyClass实例
- std::shared_ptr<MyClass> sptr2 = sptr;
-
- // 调用doSomething方法
- sptr->doSomething();
-
- // 当所有shared_ptr实例都超出作用域时,MyClass的析构函数才会被调用
- return 0;
- }
In usestd::shared_ptr
Be careful to avoid circular references when using , which will cause the reference count to never drop to 0, thus causing resource leaks. In the case of circular references, you can usestd::weak_ptr
To break the cycle,std::weak_ptr
Do not increase the reference count, just hold thestd::shared_ptr
Manages weak references to resources.
These examples show how to use smart pointers to manage dynamically allocated memory to ensure that resources are properly released when appropriate, avoiding common errors that can occur when managing memory manually.
https://www.cnblogs.com/god-of-death/p/17962676
https://zhuanlan.zhihu.com/p/678722914