马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
C++ Boost 智能指针教程
Boost 提供了一些强大的智能指针工具,用于安全和高效地管理动态内存。相比原始指针(raw pointer),Boost 的智能指针不但简化了内存管理,还制止了常见的内存走漏问题。
1. 智能指针简介
智能指针是一种封装了指针的类,可以自动管理动态内存的生命周期,制止手动开释内存。Boost 提供的智能指针包罗以下范例:
- boost::shared_ptr:共享全部权的智能指针,基于引用计数。
- boost::weak_ptr:弱引用智能指针,与 boost::shared_ptr 配合使用,办理循环引用问题。
- boost::scoped_ptr(已废弃):独占全部权,简单易用,C++11 后被 std::unique_ptr 取代。
- boost::intrusive_ptr:高效的智能指针,需用户手动管理引用计数。
2. boost::shared_ptr
特点
- 支持多个 shared_ptr 共享同一对象的全部权。
- 使用引用计数管理资源,引用计数为 0 时开释对象。
2.1 创建与使用
示例:基本用法
- #include <boost/shared_ptr.hpp>
- #include <iostream>
- int main() {
- boost::shared_ptr<int> sp1(new int(10)); // 创建 shared_ptr
- std::cout << "Value: " << *sp1 << std::endl;
- boost::shared_ptr<int> sp2 = sp1; // 共享所有权
- std::cout << "Use count: " << sp1.use_count() << std::endl;
- return 0; // 最后一个 shared_ptr 释放对象
- }
复制代码 输出
2.2 常用方法
- use_count():获取当前共享对象的引用计数。
- reset():开释对象或更换管理的对象。
- get():获取原始指针。
示例:reset 和 get
- #include <boost/shared_ptr.hpp>
- #include <iostream>
- int main() {
- boost::shared_ptr<int> sp1(new int(42));
- std::cout << "Value: " << *sp1 << ", Use count: " << sp1.use_count() << std::endl;
- sp1.reset(); // 释放对象
- std::cout << "After reset, Use count: " << sp1.use_count() << std::endl;
- boost::shared_ptr<int> sp2(new int(100));
- std::cout << "Raw pointer: " << sp2.get() << std::endl;
- return 0;
- }
复制代码 2.3 与 STL 容器的联合
boost::shared_ptr 可以安全地存储在 STL 容器中,制止重复开释内存。
示例:在 std::vector 中使用
- #include <boost/shared_ptr.hpp>
- #include <vector>
- #include <iostream>
- int main() {
- std::vector<boost::shared_ptr<int>> vec;
- vec.push_back(boost::shared_ptr<int>(new int(10)));
- vec.push_back(boost::shared_ptr<int>(new int(20)));
- for (const auto& sp : vec) {
- std::cout << *sp << " ";
- }
- return 0;
- }
复制代码 输出
3. boost::weak_ptr
特点
- 提供对 boost::shared_ptr 管理对象的非全部权访问。
- 不增长引用计数,制止循环引用问题。
3.1 基本用法
示例:办理循环引用
- #include <boost/shared_ptr.hpp>
- #include <boost/weak_ptr.hpp>
- #include <iostream>
- struct Node {
- boost::shared_ptr<Node> next;
- ~Node() {
- std::cout << "Node destroyed" << std::endl;
- }
- };
- int main() {
- boost::shared_ptr<Node> node1(new Node());
- boost::shared_ptr<Node> node2(new Node());
- node1->next = node2;
- node2->next = node1; // 循环引用,无法释放内存
- return 0; // 内存泄漏
- }
复制代码 通过 boost::weak_ptr 办理:
- #include <boost/shared_ptr.hpp>
- #include <boost/weak_ptr.hpp>
- #include <iostream>
- struct Node {
- boost::weak_ptr<Node> next; // 使用 weak_ptr 打破循环
- ~Node() {
- std::cout << "Node destroyed" << std::endl;
- }
- };
- int main() {
- boost::shared_ptr<Node> node1(new Node());
- boost::shared_ptr<Node> node2(new Node());
- node1->next = node2;
- node2->next = node1; // 不会增加引用计数
- return 0; // 正常释放内存
- }
复制代码 3.2 常用方法
- lock():返回指向共享对象的 boost::shared_ptr,假如对象已开释,则返回空指针。
- expired():查抄对象是否已被开释。
示例:lock 和 expired
- #include <boost/shared_ptr.hpp>
- #include <boost/weak_ptr.hpp>
- #include <iostream>
- int main() {
- boost::shared_ptr<int> sp1(new int(100));
- boost::weak_ptr<int> wp = sp1;
- if (auto sp2 = wp.lock()) { // 检查对象是否有效
- std::cout << "Shared object is valid: " << *sp2 << std::endl;
- }
- sp1.reset(); // 释放对象
- if (wp.expired()) {
- std::cout << "Shared object has been destroyed." << std::endl;
- }
- return 0;
- }
复制代码 4. boost::scoped_ptr(已废弃)
boost::scoped_ptr 是一种独占全部权的智能指针,无法复制或转移。C++11 后被 std::unique_ptr 取代。
5. boost::intrusive_ptr
特点
- 必要用户手动实现引用计数的增长和减少。
- 相比 boost::shared_ptr 更加高效,适合性能关键的场景。
示例:自定义引用计数
- #include <boost/intrusive_ptr.hpp>
- #include <iostream>
- class MyClass {
- int ref_count = 0;
- friend void intrusive_ptr_add_ref(MyClass* p) {
- ++p->ref_count;
- }
- friend void intrusive_ptr_release(MyClass* p) {
- if (--p->ref_count == 0) {
- delete p;
- }
- }
- public:
- void display() {
- std::cout << "MyClass instance!" << std::endl;
- }
- };
- int main() {
- boost::intrusive_ptr<MyClass> ptr1(new MyClass());
- boost::intrusive_ptr<MyClass> ptr2 = ptr1; // 引用计数增加
- ptr1->display();
- ptr2.reset(); // 引用计数减少
- ptr1.reset(); // 对象被销毁
- return 0;
- }
复制代码 6. boost::make_shared
boost::make_shared 是一种高效的方式,用于创建 boost::shared_ptr。
示例
- #include <boost/make_shared.hpp>
- #include <iostream>
- int main() {
- auto sp = boost::make_shared<int>(42); // 高效创建 shared_ptr
- std::cout << "Value: " << *sp << std::endl;
- return 0;
- }
复制代码 7. 总结与选择
范例主要特点使用场景boost::shared_ptr基于引用计数,支持多个共享全部权多个对象共享动态资源时boost::weak_ptr不增长引用计数,制止循环引用配合 shared_ptr,办理循环引用问题boost::scoped_ptr独占全部权,无法转移(已被废弃)简单的 RAII 场景(保举使用 std::unique_ptr)boost::intrusive_ptr手动管理引用计数,更高效性能关键场景,必要用户实现引用计数功能 学习发起
- 从基础开始:
- 学习 boost::shared_ptr 和 boost::weak_ptr 的基本用法。
- 理解引用计数的原理。
- 逐步深入:
- 在项目中联合 STL 容器使用智能指针。
- 相识 boost::intrusive_ptr 的高级用法。
- 参考文档:
通过体系学习这些智能指针,你将能更高效地管理动态内存,制止内存走漏问题!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |