本文共 4352 字,大约阅读时间需要 14 分钟。
智能指针(Smart Pointer)是一种用于管理动态内存的工具,能够自动释放资源,避免内存泄漏和错误操作。传统内存管理方式容易导致资源泄漏或双重释放等问题,而智能指针通过 RAII(资源 管理即初始化)原理,利用对象生命周期来管理资源。
delete
,甚至在异常发生时也无法保证资源的释放。malloc
或 new
分配的空间如果未正确释放,会导致内存泄漏,影响程序性能和安全性。new
、malloc
、realloc
分配的空间。智能指针通过 RAII 原理,利用对象的生命周期管理资源。其行为类似于指针,支持普通指针的操作,如 *
和 ->
。
RAII(Resource Acquisition Is Initialization)是一种资源管理方法,强制将资源与对象的生命周期绑定。资源通过对象的构造函数获取,对象的析构函数负责释放资源。
auto_ptr
auto_ptr
是 C++11 标新引入的智能指针类型,采用资源转移实现。
templateclass auto_ptr {public: auto_ptr(t* ptr = nullptr) : _ptr(ptr) {} ~auto_ptr() { if (_ptr) { delete _ptr; _ptr = nullptr; } } t& operator*() { return *_ptr; } t* operator->() { return _ptr; } template auto_ptr(auto_ptr & other) : _ptr(other._ptr) { other._ptr = nullptr; } template auto_ptr& operator=(auto_ptr & other) { if (this != &other) { if (_ptr) delete _ptr; _ptr = other._ptr; other._ptr = nullptr; } return *this; }private: t* _ptr;};
free
或 malloc
分配的内存。auto_ptr
会导致行为错误。unique_ptr
unique_ptr
是 auto_ptr
的改进版,采用资源独占方式。
unique_ptr
管理。unique_ptr
会将资源释放,依据新获取的对象进行资源管理。templateclass Delete {public: void operator()(t* p) { if (p) { delete p; p = nullptr; } }};// 简单实现template class unique_ptr {public: unique_ptr(t* ptr = nullptr) : _ptr(ptr) {} ~unique_ptr() { if (_ptr) DF df(_ptr); delete _ptr; _ptr = nullptr; } template unique_ptr(unique_ptr & other) = delete; template unique_ptr& operator=(unique_ptr & other) = delete; t& operator*() { return *_ptr; } t* operator->() { return _ptr; }private: t* _ptr;};
malloc
、realloc
或 free
分配的内存。shared_ptr
shared_ptr
是以引用计数方式管理共享资源的智能指针。
#include#include std::shared_ptr sp1(new int(42));std::shared_ptr sp2(sp1);std::cout << sp1.use_count() << std::endl; // 输出 1std::cout << sp2.use_count() << std::endl; // 输出 1sp2 = std::shared_ptr (new int(10));std::cout << sp1.use_count() << std::endl; // 输出 1std::cout << sp2.use_count() << std::endl; // 输出 1
shared_ptr
是否支配资源?bool own = std::shared_ptr ::owns_ptr(sp_np);
weak_ptr
用于解决 shared_ptr
在循环引用中的引用计数异常增大问题。
shared_ptr
类似,但引用计数较低。shared_ptr
的来源。class Node {public: Node(int data) : left(nullptr), right(nullptr) {} virtual ~Node() {} templateNode& operator<<(T&& t) { this->data = std::move(t); return *this; } Node* left; Node* right; int data;};
weak_ptr
实现示例template+=''class weak_ptr {public: weak_ptr(t* ptr = nullptr) : _ptr(ptr) {} template weak_ptr(weak_ptr & other) : _ptr(other._ptr) {} template weak_ptr& operator=(weak_ptr & other) { if (this != &other) { _ptr = other._ptr; } return *this; } t* operator*() { return _ptr; } t* operator->() { return _ptr; } int use_count() { return _pCount; } bool unique() { return _pCount == 0; }private: t* _ptr; int* _pCount;};
weak_ptr
为节点对象管理指针,避免循环引用导致的高引用计数。struct Node { Node(int value) : left(nullptr), right(nullptr) {} templateNode& operator<<(T&& val) { this->value = val; return *this; } weak_ptr left; weak_ptr right; int value;};
智能指针通过 RAII 原理,大大简化了内存管理,减少了内存泄漏和错误的风险。在实际应用中,根据资源管理需求选择合适的智能指针类型,如 unique_ptr
、shared_ptr
或 weak_ptr
,以确保程序的稳定性和安全性。
转载地址:http://dsacz.baihongyu.com/