抬一手C++的智能指针
前段时间学了一些项目的C++特性之类的,简单总结一下吧,以前发现写个智能指针之类的很麻烦,其实现在知识体系够了之后发现也没那么麻烦。
首先你需要知道这玩意是为了干啥?
比如说面试官问你 介绍 一下你了解的C++智能指针你怎么说
有三类,分别干了啥,有啥用......
以前我们可能要自己new 跟 delete,当然也没什么,但是如果在delete前出现了一个问题,后面不跑delete了,这就会内存泄露
还有就是delete只是告诉操作系统这块内存不归我管了,里面的内容可能依旧没有改变
多次delete相同内存会直接导致程序玩完。
为了避免这个才有的智能指针,它仅仅是一个思想,就是我们用它来屏蔽掉我们上面的那些情况。
它就是为了处理内存问题的,它是一个类,底层就是对指针的一个封装。
为什么要用类去封装呢,因为class有默认的构造析构函数,析构 会在作用周期结束后自动调用,那这样就解决上面的问题了。
---(其实这才是你对这个问题的思考过程,是不是比起你背的面积通俗很多,还能体现出你个人的思考)
所以这个核心在于这个类的各种函数怎么去实现。
实现这个之前我们需要了解 泛型编程、拷贝构造函数、赋值构造函数、移动语义构造函数、指针操作(也就是当前拥有这个内存的个数)
自己写一个计数的share_ptr 大概如下。 改成unique的话就把拷贝构造、复制构造 = delete就行。 default 也可以去了解一下。
仿佛明白今年春招的时候, 小红书的面试官 为什么对我那么疑惑了~
当你搞完这些,再去看看cpp的内存管理,就可以在简历上加上一行了解c++的内存管理与智能指针~
/* c++内存管理与智能指针。 */ #include <iostream> #include <string> #include <memory> #include <vector> #include <algorithm> #include <sys/time.h> template<typename T> class SmartPointer { public: SmartPointer() { // 构造 m_pointer = nullptr; m_count = nullptr; std::cout << "null construction" << '\n'; } SmartPointer(T* p) { // 通过指针对象构造 m_pointer = p; if(p != nullptr) { m_count = new int (1); } else { m_count = nullptr; } std::cout << "construction with pointer" << '\n'; } SmartPointer(const SmartPointer &other) { // 通过其他指针拷贝构造 m_pointer = other.m_pointer; m_count = other.m_count; if(m_count != nullptr) { *m_count += 1; } std::cout << "copy with other smartpointer" << '\n'; } SmartPointer& operator = (const SmartPointer& other) { // 赋值运算 // now = other 原来的now减少一次 然后换成other if(this == &other) { return *this; } deconstruction(); m_pointer = other.m_pointer; m_count = other.m_count; if(m_count != nullptr) { *m_count += 1; } std::cout << "a = b" << '\n'; return *this; } ~SmartPointer() { deconstruction(); } void deconstruction() { // 析构 if(m_count != nullptr){ std::cout << "number--" << '\n'; if(*m_count >= 1) { *m_count -= 1; } if(*m_count == 0) { delete m_pointer; delete m_count; std::cout << "realse pointer" << '\n'; } } } T getPointer() { if(m_pointer == nullptr) { throw "null error"; } return *m_pointer; } int getCount() { return m_count == nullptr ? 0 : *m_count; } private: T* m_pointer; int* m_count; }; void testSmartPointer(){ SmartPointer<int> pointer = new int (3); // 利用指针对象构造 std::cout << pointer.getPointer() << '\n'; std::cout << pointer.getCount() << '\n'; SmartPointer<int> secondPoiner = pointer; // 拷贝构造 SmartPointer<int> nullPointer; // 空构造 nullPointer = secondPoiner; // 赋值操作 std::cout << secondPoiner.getPointer() << '\n'; std::cout << secondPoiner.getCount() << '\n'; // 此时三个指针都指向同一对象 SmartPointer<int> thiredPointer; secondPoiner = thiredPointer; // 把一个指针指向其他内容, 减少计数 std::cout << pointer.getPointer() << '\n'; std::cout << pointer.getCount() << '\n'; // 空指针调用getPointer方***有异常~ } class B; class A { public: std::shared_ptr<B> ptr; //std::weak_ptr<B> ptr; ~A(){ std::cout << "delete A" << std::endl; } }; class B { public: std::shared_ptr<A> ptr; ~B(){ std::cout << "delete B" << std::endl; } }; int main() { //testSmartPointer(); /* std::unique_ptr<int> ptr(new int (3)); std::cout << *ptr << std::endl; std::cout << &ptr << std::endl; std::unique_ptr<int> ptr2 = std::move(ptr); std::cout << *ptr2 << std::endl; std::cout << &ptr2 << std::endl; ptr = std::move(ptr2); std::cout << *ptr << std::endl; std::cout << &ptr << std::endl; std::shared_ptr<int>ptr = std::make_shared<int>(10); std::cout << ptr.use_count() << std::endl; std::cout << *ptr.get() << std::endl; std::shared_ptr<int>ptr2 = ptr; std::cout << *ptr2.get() << std::endl; *ptr.get() = 20; std::cout << *ptr2.get() << std::endl; ptr.reset(); std::cout << ptr2.use_count() << std::endl; */ // { // std::shared_ptr<A> pa(new A()); // std::shared_ptr<B> pb(new B()); // pa->ptr = pb; // pb->ptr = pa; // std::shared_ptr<int> x = std::make_shared<int>(3); // std::cout << sizeof(pa) << std::endl; // } return 0; }