C++11引入std::unique_ptr和std::shared_ptr管理动态内存,前者独占所有权、不可复制但可移动,适用于局部资源;后者通过引用计数实现共享所有权,需配合std::weak_ptr避免循环引用,推荐使用std::make_unique和std::make_shared创建,提升安全与性能。

在C++中,智能指针是管理动态内存的现代方式,能有效避免内存泄漏和资源管理错误。C++11引入了 std::shared_ptr 和 std::unique_ptr,它们都定义在 头文件中。下面详细介绍它们的用法与最佳实践。
std::unique_ptr:独占式所有权
std::unique_ptr 表示对所指向对象的唯一所有权。同一时间只能有一个 unique_ptr 指向某个对象,不能复制,但可以移动。
使用 std::make_unique 创建(C++14起支持):
#include <memory> #include <iostream> auto ptr = std::make_unique<int>(42); std::cout <p>如果使用旧版本C++(如C++11),可用 <code>new</code> 构造:</p> <pre class="brush:php;toolbar:false;">std::unique_ptr<int> ptr(new int(42)); </int>移动语义:
不能复制,但可移动:
auto ptr1 = std::make_unique<int>(10); // auto ptr2 = ptr1; // 错误:不能复制 auto ptr2 = std::move(ptr1); // 正确:转移所有权 // 此时 ptr1 为空,ptr2 拥有对象 </int>用于类成员或函数返回:
std::unique_ptr<int> createValue() {
return std::make_unique<int>(100);
}
</int></int>
适用于工厂模式、资源封装等场景。
std::shared_ptr:共享式所有权
std::shared_ptr 使用引用计数机制,多个指针可共享同一个对象。当最后一个 shared_ptr 被销毁时,对象自动释放。
推荐使用 std::make_shared:
auto sptr = std::make_shared<int>(50); std::cout <p>多个 <code>shared_ptr</code> 可指向同一对象:</p><div class="aritcle_card flexRow artxards"> <div class="artcardd flexRow"> <a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill10691" title="Livestream Script Generator"><img src="https://img.php.cn/upload/skill/001/246/273/6adaf20e695c2374.jpg" alt="Livestream Script Generator" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a> <div class="aritcle_card_info flexColumn"> <a rel="nofollow" href="/xiazai/skill10691" title="Livestream Script Generator" class="overflowclass">Livestream Script Generator</a> <p class="overflowclass">直播带货脚本:话术、节奏、互动、促单。适用于主播、电商运营、MCN机构。</p> </div> <a rel="nofollow" href="/xiazai/skill10691" title="Livestream Script Generator" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a> </div> </div> <pre class="brush:php;toolbar:false;">auto sp1 = std::make_shared<int>(88); auto sp2 = sp1; // 引用计数 +1 auto sp3 = sp1; // 引用计数 +1 // 此时引用计数为 3 </int>
离开作用域后,引用计数递减,归零时自动 delete。
性能提示:make_shared 比直接 new 更高效,因为它将控制块和对象一起分配,减少内存碎片和分配次数。
避免循环引用:使用 weak_ptr
当两个 shared_ptr 相互持有对方时,会形成循环引用,导致内存无法释放。
struct Node {
std::shared_ptr<node> parent;
std::shared_ptr<node> child;
};
</node></node>
上面代码中,parent 和 child 互相引用,即使超出作用域,引用计数也不为0。
解决方案:使用 std::weak_ptrweak_ptr 不增加引用计数,用于观察 shared_ptr 所管理的对象。
struct Node {
std::weak_ptr<node> parent; // 改为 weak_ptr
std::shared_ptr<node> child;
};
</node></node>
访问时需升级为 shared_ptr:
if (auto p = parent.lock()) {
// 安全访问父节点
std::cout <h3>内存管理技巧与最佳实践</h3><p>合理使用智能指针,可以极大提升代码安全性和可维护性。</p>
- 优先使用 make_unique 和 make_shared:避免裸 new,更安全且性能更好。
- 不要混合管理:一个原始指针不要同时被多个智能指针管理,否则会导致重复释放。
-
避免从 this 创建 shared_ptr:若类本身是 shared_ptr 管理的,应继承
std::enable_shared_from_this:
struct MyClass : std::enable_shared_from_this<myclass> {
std::shared_ptr<myclass> getSelf() {
return shared_from_this();
}
};
</myclass></myclass>
- unique_ptr 适合大多数局部资源管理:如函数内动态对象、类的私有成员等。
- shared_ptr 用于需要共享所有权的场景:如缓存、观察者模式、树结构中的子节点等。
基本上就这些。掌握 unique_ptr 和 shared_ptr 的核心用法,并注意避免循环引用,就能写出既高效又安全的C++代码。
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










