经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
4.2 C++ Boost 内存池管理库
来源:cnblogs  作者:lyshark  时间:2023/8/18 9:36:47  对本文有异议

Boost 库是一个由C/C++语言的开发者创建并更新维护的开源类库,其提供了许多功能强大的程序库和工具,用于开发高质量、可移植、高效的C应用程序。Boost库可以作为标准C库的后备,通常被称为准标准库,是C标准化进程的重要开发引擎之一。使用Boost库可以加速C应用程序的开发过程,提高代码质量和性能,并且可以适用于多种不同的系统平台和编译器。Boost库已被广泛应用于许多不同领域的C++应用程序开发中,如网络应用程序、图像处理、数值计算、多线程应用程序和文件系统处理等。

C++的指针操作可以说是继承了C语言的优点,但同时也带来了一些问题,例如内存泄漏、悬挂指针、访问越界等。这些问题不仅会导致程序运行错误,还会对系统稳定性造成影响。为了避免这些问题,Boost库提供了一套高效的自动内存管理指针操作函数,这些函数使用引用计数技术来管理内存。

2.1 使用Pool内存池

boost::pool是Boost库中一个内存池管理器,用于高效地管理和分配内存。在程序中,动态分配和释放内存是很常见的操作,但频繁的内存分配和释放会导致开销很大,影响程序性能。boost::pool针对这个问题提供了一个解决方案,它可以预分配并缓存一定数量的内存块,通过重复利用这些内存块来减小内存分配释放的开销,提高程序性能。

  1. #include <iostream>
  2. #include <boost/pool/pool.hpp>
  3. using namespace std;
  4. using namespace boost;
  5. int main(int argc, char const *argv[])
  6. {
  7. boost::pool<> pool(sizeof(int)); // 定义整数内存池(int/float/double)
  8. int *ptr[10] = { 0 }; // 定义指针列表
  9. for (int x = 0; x < 10; x++)
  10. {
  11. ptr[x] = static_cast<int *>(pool.malloc()); // 开辟空间并转为指针
  12. if (ptr[x] == nullptr)
  13. cout << "分配空间失败" << endl;
  14. }
  15. // 分别对内存空间赋值
  16. for (int x = 0; x < 10; x++)
  17. *ptr[x] = x;
  18. // 输出数据
  19. for (int x = 0; x < 10; x++)
  20. {
  21. cout << "内存地址: " << &ptr[x] << " 数值: " << *ptr[x] << endl;
  22. }
  23. getchar();
  24. return 0;
  25. }

Pool内存池同样提供了对容器的存储方法,我们在使用时只需要包含头文件pool_alloc.hpp,当包含此头文件后读者可使用pool_allocator模板类对容器内的特殊成员进行初始化。

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <boost/pool/pool.hpp>
  5. #include <boost/pool/pool_alloc.hpp>
  6. using namespace std;
  7. using namespace boost;
  8. typedef struct
  9. {
  10. int uuid;
  11. string uname;
  12. }MyStruct;
  13. int main(int argc, char const *argv[])
  14. {
  15. // 应用标准容器: 定义存储string类型的容器
  16. std::vector<std::string, pool_allocator<std::string> > vect;
  17. // 设置容器
  18. vect.push_back("admin");
  19. vect.push_back("lyshark");
  20. for (int x = 0; x < vect.size(); x++)
  21. {
  22. std::cout << "输出: " << vect[x] << std::endl;
  23. }
  24. // 应用自定义数据类型
  25. std::vector<MyStruct, pool_allocator<MyStruct>> pool_ptr;
  26. MyStruct ptr;
  27. ptr.uuid = 10001;
  28. ptr.uname = "lyshark";
  29. pool_ptr.push_back(ptr);
  30. ptr.uuid = 1002;
  31. ptr.uname = "admin";
  32. pool_ptr.push_back(ptr);
  33. for (int x = 0; x < pool_ptr.size(); x++)
  34. {
  35. std::cout << "UUID: " << pool_ptr[x].uuid << " Name: " << pool_ptr[x].uname << std::endl;
  36. }
  37. std::system("pause");
  38. return 0;
  39. }

2.2 使用ObjectPool内存池

boost::object_pool是Boost库中的一个内存池管理器,可以用来高效地分配和释放内存,并能够管理多个大小相等的对象。

在使用boost::object_pool时,我们可以先创建一个大小固定的内存池,然后使用malloc()函数从内存池中分配内存,并在内存上构造一个对象。当我们需要释放内存时,可以调用destroy()函数显式地销毁对象,并使用free()函数释放内存。

  1. #include <iostream>
  2. #include <string>
  3. #include <boost/pool/object_pool.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. struct MyStruct
  7. {
  8. public:
  9. int uuid;
  10. string uname;
  11. int uage;
  12. MyStruct(int uuid_, string uname_, int uage_)
  13. {
  14. uuid = uuid_; uname = uname_; uage = uage_;
  15. }
  16. };
  17. int main(int argc, char const *argv[])
  18. {
  19. boost::object_pool<MyStruct> object;
  20. auto ptr = object.malloc();
  21. // 默认最多只能传递3个参数
  22. ptr = object.construct(1001,"lyshark",25); // 为构造函数传递参数
  23. cout << "姓名: " << ptr->uname << endl;
  24. std::system("pause");
  25. return 0;
  26. }

一般在默认情况下object_pool内存池只能接收三个以内的参数传递,当读者需要使用多于三个参数时则需要使用自定义可变参数模板来实现功能,我们以接受四个参数为例,定义construct模板并在该模板内部实现分配资源。

  1. #include <iostream>
  2. #include <string>
  3. #include <boost/pool/object_pool.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. struct MyStruct
  7. {
  8. public:
  9. int uuid;
  10. string uname;
  11. int uage;
  12. string usex;
  13. MyStruct(int uuid_, string uname_, int uage_, string usex_)
  14. {
  15. uuid = uuid_; uname = uname_; uage = uage_; usex = usex_;
  16. }
  17. };
  18. // 定义可变参数模板,用来实现接受三个以上的参数
  19. template<typename P, typename ... Args> inline typename P::element_type* construct(P& p, Args&& ... args)
  20. {
  21. typename P::element_type* mem = p.malloc();
  22. new(mem) typename P::element_type(std::forward<Args>(args)...);
  23. return mem;
  24. }
  25. int main(int argc, char const *argv[])
  26. {
  27. boost::object_pool<MyStruct> object;
  28. auto ptr = object.malloc();
  29. // 接收四个参数写法
  30. auto ref = construct(object, 1001, "lyshark", 24, "男");
  31. cout << "姓名: " << ref->uname << endl;
  32. object.free(ref);
  33. object.free(ptr);
  34. std::system("pause");
  35. return 0;
  36. }

2.3 使用SharedPtr智能指针

boost::shared_ptr是Boost库中的一个智能指针,用于自动管理动态分配的内存。它跟踪有多少个shared_ptr实例共享同一个对象,当最后一个实例离开作用域时,它会自动释放分配的内存。

该函数是boost.smart_ptr库中最重要的智能指针,shared_ptr包装了new操作符在堆上分配的动态对象,实现了引用计数型的智能指针,可被自由的拷贝和赋值,并在任意地方共享。

  1. #include <iostream>
  2. #include <string>
  3. #include <boost/smart_ptr.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. int main(int argc, char const *argv[])
  7. {
  8. // 基本的定义与赋值
  9. boost::shared_ptr<int> int_ptr(new int);
  10. *int_ptr = 1024;
  11. cout << "指针: " << &int_ptr << " 数值: " << *int_ptr << endl;
  12. boost::shared_ptr<string> string_ptr(new string);
  13. *string_ptr = "hello lyshark";
  14. cout << "指针: " << &string_ptr << " 长度: " << string_ptr->size() << endl;
  15. // 拷贝构造的使用
  16. boost::shared_ptr<int> shared_ptr(new int(10)); // 定义指向整数的shared
  17. cout << "持有者: " << shared_ptr.unique() << endl;
  18. boost::shared_ptr<int>shared_copy = shared_ptr; // 实现拷贝
  19. cout << "引用数: " << shared_ptr.use_count() << endl;
  20. shared_ptr.reset(); // 关闭shared的使用
  21. getchar();
  22. return 0;
  23. }

在有时候我们需要使用多个指针,并将多个指针分别指向不同的数据集合,此时我们可以先封装一个MyShared类,并使用循环的方式初始化创建内存空间,每次创建空间后将该空间存储至vect容器内,最后再以此循环输出该容器内存所有自定义类元素即可;

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <boost/shared_ptr.hpp>
  5. using namespace std;
  6. using namespace boost;
  7. // 定义Shared类
  8. class MyShared
  9. {
  10. private:
  11. int shared_uuid;
  12. std::string shared_name;
  13. public:
  14. MyShared(int x, std::string y)
  15. {
  16. shared_uuid = x;
  17. shared_name = y;
  18. }
  19. std::string GetName()
  20. {
  21. return shared_name;
  22. }
  23. int GetUUID()
  24. {
  25. return shared_uuid;
  26. }
  27. };
  28. int main(int argc, char const *argv[])
  29. {
  30. std::vector<boost::shared_ptr<MyShared>> vect;
  31. // 循环开辟空间,并放入vector列表
  32. for (int x = 0; x < 5; x++)
  33. {
  34. boost::shared_ptr<MyShared> ptr(new MyShared(x,"hello lyshark"));
  35. vect.push_back(ptr);
  36. }
  37. // 输出列表中的元素
  38. for (int x = 0; x < vect.size(); x++)
  39. {
  40. std::cout << "UUID: " << vect[x]->GetUUID() << " Name: " << vect[x]->GetName() << std::endl;
  41. }
  42. std::system("pause");
  43. return 0;
  44. }

智能指针同样支持使用引用计数器功能,在指针内部读者可通过使用ptr.use_count()来输出当前的计数器,当此处代码没有被使用是则引用计数器会为0,而当代码或多个进程使用时则引用计数器相应的会增加,查询引用计数器可以如下所示;

  1. #include <iostream>
  2. #include <string>
  3. #include <boost/shared_ptr.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. class MyShared
  7. {
  8. private:
  9. boost::shared_ptr<int> ptr;
  10. public:
  11. MyShared(boost::shared_ptr<int> p_) :ptr(p_){}
  12. void print()
  13. {
  14. cout << "内部 计数: " << ptr.use_count() << " 数值: " << *ptr << endl;
  15. }
  16. };
  17. // 自动拷贝一个对象,所以引用计数会+1
  18. void print_func(boost::shared_ptr<int> ptr)
  19. {
  20. cout << "外部 计数: " << ptr.use_count() << " 数值: " << *ptr << endl;
  21. }
  22. int main(int argc, char const *argv[])
  23. {
  24. boost::shared_ptr<int> ptr(new int(100)); // 定义整数指针
  25. MyShared s1(ptr), s2(ptr); // 定义两个对象,并初始化
  26. s1.print();
  27. s2.print();
  28. *ptr = 200;
  29. print_func(ptr);
  30. s1.print();
  31. s2.print();
  32. std::system("pause");
  33. return 0;
  34. }

如上,在声明了shared_ptr和两个MyShared类后,指针被共享,因此引用计数为3,调用print_func()函数,该函数内部拷贝了一个shared_ptr对象,因此引用计数再次增加1,但退出函数时,拷贝自动析构,引用计数又会恢复为3。

2.4 使用MakeShared工厂函数

boost::make_shared是一个工厂函数,用于动态分配一个对象并返回一个智能指针,它是Boost库中的一个组件。使用make_shared我们可以将对象的构造和内存分配合并在一起,避免了常规构造函数和动态内存分配的性能损失和代码冗余。

当读者使用2.3节中所示的shared_ptr智能指针时,虽然能够很好的消除delete释放的调用,但我们还是需要使用new方法来构造初始化数据集,为了能够不再使用new关键字,在smart_ptr库中提供了一个工厂函数make_shared()函数,用于消除使用new创建数据集,工厂函数常用于初始化特定的指针数据,如下所示;

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <boost/smart_ptr.hpp>
  5. using namespace std;
  6. using namespace boost;
  7. int main(int argc, char const *argv[])
  8. {
  9. // make_shared 工厂函数初始化
  10. boost::shared_ptr<string> string_ptr = boost::make_shared<string>("hello lyshark");
  11. cout << "初始化字符串: " << *string_ptr << endl;
  12. // 应用于标准容器中
  13. typedef std::vector<boost::shared_ptr<int>> vector_ptr; // 定义标准容器类型
  14. vector_ptr vect(10); // 定义拥有十个元素的容器
  15. // 初始化赋值
  16. int x = 0;
  17. for (auto pos = vect.begin(); pos != vect.end(); ++pos)
  18. {
  19. (*pos) = boost::make_shared<int>(++x); // 工厂函数初始化
  20. cout << "输出值: " << *(*pos) << endl; // 输出数据(两次解引用)
  21. }
  22. // 修改数据
  23. boost::shared_ptr<int> int_ptr = vect[9]; // 获取最后一个数值
  24. *int_ptr = 100; // 修改最后一个数值
  25. cout << "修改后: " << *vect[9] << endl;
  26. // 第二种输出方式(一次解引用完成)
  27. x = 0;
  28. for (auto& ptr : vect)
  29. {
  30. cout << "输出值: " << *ptr << endl;
  31. }
  32. std::system("pause");
  33. return 0;
  34. }

2.5 使用SharedPtr桥接模式

在C++中,shared_ptr有一种常用的设计模式是桥接模式(Bridge Design Pattern)又称为PIMPL模式。桥接模式的主要作用是将实现细节从类的接口中分离出来,从而使得接口和实现可以独立变化,提高了类的可扩展性和可维护性。

使用shared_ptr实现桥接模式时,我们可以使用一个基类和多个派生类的继承关系,并使用shared_ptr来管理对象的生命周期。通过使用shared_ptr的引用计数技术,可以动态地改变派生类的具体实现,而不会影响到基类接口的实现。其仅对外部暴漏最小的细节,内部类实现用一个shared_ptr来保存指针。

如下代码所示,首先我们定义MyShared作为基类,其内部存在一个print输出函数,而该函数通过boost::shared_ptr<impl> ptr;指向impl基址,当输出内容时,自动桥接到impl派生类上的print函数上。

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <boost/smart_ptr.hpp>
  5. using namespace std;
  6. using namespace boost;
  7. // 定义基类
  8. class MyShared
  9. {
  10. private:
  11. class impl;
  12. boost::shared_ptr<impl> ptr;
  13. public:
  14. MyShared();
  15. void print();
  16. };
  17. // 定义桥接类
  18. class MyShared::impl
  19. {
  20. public:
  21. void print()
  22. {
  23. cout << "输出桥接类" << endl;
  24. }
  25. };
  26. MyShared::MyShared() :ptr(new impl){}
  27. void MyShared::print()
  28. {
  29. ptr->print();
  30. }
  31. int main(int argc, char const *argv[])
  32. {
  33. MyShared lsp;
  34. lsp.print();
  35. std::system("pause");
  36. return 0;
  37. }

2.6 使用SharedPtr工厂模式

在C++中,shared_ptr还可以与工厂模式(Factory Design Pattern)结合使用,工厂模式是一种创建型设计模式,该模式包装了new操作符的使用,使对象的创建工作集中在工厂类或工厂函数上,通过创建和返回智能指针,从而实现动态创建对象并自动管理其生命周期的功能。

通常开发中,自己编写的工厂类都会在堆上使用new动态分配对象,然后返回对象指针,当忘记释放delete时,内存泄漏就会产生。当使用shared_ptr实现工厂模式时,我们可以将工厂类中的创建对象的方法返回一个shared_ptr对象,从而避免手动管理动态分配的内存。

如下代码所示,我们使用shared_ptr封装接口,让impl类不再返回原始指针,而是返回shared_ptr包装的智能指针,这样就可以很好的保护资源。

  1. #include <iostream>
  2. #include <boost/smart_ptr.hpp>
  3. using namespace std;
  4. using namespace boost;
  5. // 定义基类 全部为虚函数
  6. class abstract
  7. {
  8. public:
  9. virtual void MyPrint() = 0;
  10. protected:
  11. virtual ~abstract() = default;
  12. };
  13. // 派生类实现虚函数
  14. class impl :public abstract
  15. {
  16. public:
  17. impl() = default;
  18. virtual ~impl() = default;
  19. public:
  20. virtual void MyPrint()
  21. {
  22. cout << "调用方法完成." << endl;
  23. }
  24. };
  25. // 工厂函数返回基类的 基址指针 返回类型为 shared_ptr
  26. boost::shared_ptr<abstract> create()
  27. {
  28. return boost::make_shared<impl>();
  29. }
  30. int main(int argc, char const *argv[])
  31. {
  32. // 第一种调用方式
  33. auto ptr = create();
  34. ptr->MyPrint();
  35. // 第二种方式
  36. abstract *abstract_ptr = ptr.get();
  37. abstract_ptr->MyPrint();
  38. // 强制转换,后输出
  39. impl *impl_ptr = (impl*)(ptr.get());
  40. impl_ptr->MyPrint();
  41. std::system("pause");
  42. return 0;
  43. }

2.7 使用SharedPtr资源共享

使用shared_ptr实现资源共享时,我们可以创建多个shared_ptr对象,让它们共同管理同一个动态分配的对象,从而避免了内存泄漏和错误释放内存的情况。

如下案例中我们定义了shared_vector类,当MyShared中的内容发生变化时,由于ptr指向了MyShared类,则ptr中的值也会随着MyShared中的内容的变化而变化。

  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <string>
  5. using namespace std;
  6. class shared_vector
  7. {
  8. public:
  9. typedef vector<string>::size_type size_type;
  10. shared_vector() : data(make_shared<vector<string>>()){}
  11. shared_vector(initializer_list<string> il) : data(make_shared<vector<string>>(il)){}
  12. size_type size()const{ return data->size(); }
  13. bool empty()const{ return data->empty(); }
  14. //尾部插入删除元素
  15. void push_back(const string& s){ data->push_back(s); }
  16. void pop_back(){ data->pop_back(); }
  17. //访问元素
  18. string& front(){ return data->front(); }
  19. string& back(){ return data->back(); }
  20. private:
  21. shared_ptr<vector<string>> data;
  22. };
  23. int main(int argc, char const *argv[])
  24. {
  25. shared_vector MyShared{ "admin", "lyshark" };
  26. shared_vector ptr(MyShared);
  27. ptr.push_back("administrator");
  28. cout << "发生变化: " << MyShared.back() << endl;
  29. std::system("pause");
  30. }

2.8 使用WeakPtr智能指针

weak_ptr是C++11中的智能指针,它用于解决shared_ptr可能引起的循环引用问题。与shared_ptr不同,weak_ptr并不持有所指对象的所有权,因此它不能直接访问所指向的对象。它只是提供了一种通过shared_ptr访问所指向对象的方式,并且在没有引用时可以自动弱化其引用。

在使用weak_ptr时,通常需要先从一个shared_ptr对象创建一个weak_ptr对象。我们可以通过lock()函数获取指向所指对象的shared_ptr对象,然后通过这个shared_ptr对象来访问所指对象。

如果简单来说,这个指针的出现只是为了配合shared_ptr使用的,其本身并不具备普通指针的行为,其主要的作用在于协助shared_ptr工作,像旁观者一样观察资源的使用情况。

  1. #include <iostream>
  2. #include <boost/smart_ptr.hpp>
  3. using namespace std;
  4. using namespace boost;
  5. int main(int argc, char const *argv[])
  6. {
  7. boost::shared_ptr<int> ptr(new int(10));
  8. boost::weak_ptr<int> weak(ptr);
  9. // 判断weak_ptr观察的对象是否失效
  10. if (!weak.expired())
  11. {
  12. // 获得一个shared_ptr
  13. boost::shared_ptr<int> new_ptr = weak.lock();
  14. *new_ptr = 100;
  15. }
  16. ptr.reset();
  17. std::system("pause");
  18. return 0;
  19. }

weak_ptr还可以用于对象自我管理,如获得this指针的shared_ptr使对象自己能产生shared_ptr管理自己,使用时需要定义类,并继承于enable_shared_from_this接口。

  1. #include <iostream>
  2. #include <boost/smart_ptr.hpp>
  3. #include <boost/enable_shared_from_this.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. class self_my_shared_ptr : public boost::enable_shared_from_this<self_my_shared_ptr>
  7. {
  8. public:
  9. self_my_shared_ptr(int n) :x(n){}
  10. int x;
  11. void print()
  12. {
  13. std::cout << "自身指针: " << x << std::endl;
  14. }
  15. };
  16. int main(int argc, char const *argv[])
  17. {
  18. auto ptr = boost::make_shared<self_my_shared_ptr>(100);
  19. ptr->print();
  20. auto p = ptr->shared_from_this();
  21. p->x = 200;
  22. p->print();
  23. std::system("pause");
  24. return 0;
  25. }

有时候代码中可能会出现循环引用的情况,此时使用shared_ptr指针时计数器就会失效,导致无法正确释放资源,例如如下一个案例,两个节点对象互相持有对方的引用,每个引用计数器都是2,在析构时引用计数没有变为0,因此不会调用删除清理操作,所以会导致内存泄漏的产生。

  1. #include <iostream>
  2. #include <boost/smart_ptr.hpp>
  3. #include <boost/enable_shared_from_this.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. class node
  7. {
  8. public:
  9. ~node()
  10. {
  11. std::cout << "析构函数,被调用." << std::endl;
  12. }
  13. typedef boost::shared_ptr<node> ptr_type;
  14. ptr_type next;
  15. };
  16. int main(int argc, char const *argv[])
  17. {
  18. auto ptrA = boost::make_shared<node>();
  19. auto ptrB = boost::make_shared<node>();
  20. ptrA->next = ptrB;
  21. ptrB->next = ptrA;
  22. std::cout << "ptrA 计数器: "<< ptrA.use_count() << std::endl;
  23. std::cout << "ptrB 计数器: " << ptrB.use_count() << std::endl;
  24. std::system("pause");
  25. return 0;
  26. }

为了解决上述的内存泄露问题,我们需要使用weak_ptr智能指针,将原来的强引用模式改为弱引用模式,即可实现动态释放,循环引用即可消失。

  1. #include <iostream>
  2. #include <boost/smart_ptr.hpp>
  3. #include <boost/enable_shared_from_this.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. class node
  7. {
  8. public:
  9. ~node()
  10. {
  11. std::cout << "析构函数,被调用." << std::endl;
  12. }
  13. typedef boost::weak_ptr<node> ptr_type;
  14. ptr_type next;
  15. };
  16. int main(int argc, char const *argv[])
  17. {
  18. auto ptrA = boost::make_shared<node>();
  19. auto ptrB = boost::make_shared<node>();
  20. ptrA->next = ptrB;
  21. ptrB->next = ptrA;
  22. std::cout << "ptrA 计数器: "<< ptrA.use_count() << std::endl;
  23. std::cout << "ptrB 计数器: " << ptrB.use_count() << std::endl;
  24. // 检查弱引用是否有效
  25. if (!ptrA->next.expired())
  26. {
  27. // 获取到强引用指针
  28. auto ptrC = ptrA->next.lock();
  29. }
  30. std::system("pause");
  31. return 0;
  32. }

2.9 使用IntrusivePtr计数器

intrusive_ptr是一个智能指针,与shared_ptr类似,都具有引用计数的功能。它是一个轻量级的智能指针,相比于标准库中的shared_ptr,intrusive_ptr可以方便地在自定义数据结构中使用,因为它不需要在自定义类型中维护额外的引用计数器。

该指针采用了惯用法,即将引用计数器作为自定义类型的一部分存储在实例中。因此,使用intrusive_ptr时,需要为自定义类型提供一个内部引用计数器的实现。

  1. #include <iostream>
  2. #include <boost/smart_ptr.hpp>
  3. #include <boost/smart_ptr/intrusive_ref_counter.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. struct data
  7. {
  8. int m_count = 0;
  9. ~data()
  10. {
  11. cout << "结束." << endl;
  12. }
  13. };
  14. // 递增
  15. void intrusive_ptr_add_ref(data* p)
  16. {
  17. p->m_count = p->m_count + 10;
  18. }
  19. // 递减
  20. void intrusive_ptr_release(data* p)
  21. {
  22. if (--p->m_count == 0)
  23. {
  24. delete p;
  25. }
  26. }
  27. int main(int argc,char *argv[])
  28. {
  29. // 使用自定义引用计数
  30. typedef intrusive_ptr<data> counted_ptr;
  31. counted_ptr p(new data);
  32. std::cout << "引用数: " << p->m_count << std::endl;
  33. counted_ptr weak_p(p.get(), false);
  34. std::cout << "引用数: " << p->m_count << std::endl;
  35. std::system("pause");
  36. return 0;
  37. }

本文作者: 王瑞
本文链接: https://www.lyshark.com/post/bc8ff67e.html
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

原文链接:https://www.cnblogs.com/LyShark/p/17639542.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号