文章目录
1 智能指针
1.1 智能指针的作用
智能指针的行为类似常规指针,重要的区别是它负责自动释放所指向的对象。
1.2 智能指针有哪些
智能指针在C++11版本之后提供,包含在头文件 <memory>
中,shared_ptr、unique_ptr、auto_ptr
1.3 nullptr
专用于初始化空类型指针
void f(void *c)
{
cout<<"nullptr"<<endl;
}
void f(int n)
{
cout<<"n"<<endl;
}
int main()
{
f(nullptr);
f(NULL);//#define NULL 0
f(0);
return 0;
}
// nullptr
// n
// n
1.4 shared_ptr
1.4.1 内存资源管理不当案例
- 有些内存资源已经被释放,但指向它的指针并没有改变指向(成为了野指针),并且后续还在使用;
- 有些内存资源已经被释放,后期又试图再释放一次(重复释放同一块内存会导致程序运行崩溃);
- 没有及时释放不再使用的内存资源,造成内存泄漏,程序占用的内存资源越来越多。
1.4.2 垃圾回收机制
如java的gc垃圾回收机制,很多语言都有对内存自动管理,即所谓的垃圾回收机制。所谓垃圾,指的是那些不再使用或者没有任何指针指向的内存空间,而“回收”则指的是将这些“垃圾”收集起来以便再次利用。C++也开始加入这个行列。
C++采用 unique_ptr、shared_ptr 以及 weak_ptr 这 3 个智能指针来实现堆内存的自动回收。来实现自动释放分配的内存。
底层原理:C++ 智能指针底层是采用引用计数的方式实现的。简单的理解,智能指针在申请堆内存空间的同时,会为其配备一个整形值(初始值为 1),每当有新对象使用此堆内存时,该整形值 +1;反之,每当使用此堆内存的对象被释放时,该整形值减 1。当堆空间对应的整形值为 0 时,即表明不再有对象使用它,该堆空间就会被释放掉。
1.4.3 shared_ptr指针初始化
std::shared_ptr<int> p1; //不传入任何实参
std::shared_ptr<int> p2(nullptr); //传入空指针 nullptr
std::shared_ptr<int> p3(new int(10));
std::shared_ptr<int> p3 = std::make_shared<int>(10);
std::shared_ptr<int> p4(p3);//或者 std::shared_ptr<int> p4 = p3;
对于申请的动态数组来说,shared_ptr 指针默认的释放规则是不支持释放数组的,只能自定义对应的释放规则,才能正确地释放申请的堆内存。
//指定 default_delete 作为释放规则
std::shared_ptr<int> p6(new int[10], std::default_delete<int[]>());
//自定义释放规则
void deleteInt(int*p) {
delete []p;
}
//初始化智能指针,并自定义释放规则
std::shared_ptr<int> p7(new int[10], deleteInt);
1.4.4 成员方法
#include <iostream>
#include <memory>
using namespace std;
int main()
{
//构建 2 个智能指针
std::shared_ptr<int> p1(new int(10));
std::shared_ptr<int> p2(p1);
//输出 p2 指向的数据
cout << *p2 << endl;
p1.reset();//引用计数减 1,p1为空指针
if (p1) {
cout << "p1 不为空" << endl;
}
else {
cout << "p1 为空" << endl;
}
//以上操作,并不会影响 p2
cout << *p2 << endl;
//判断当前和 p2 同指向的智能指针有多少个
cout << p2.use_count() << endl;
return 0;
}
1.5 unique_ptr
和 shared_ptr 指针最大的不同之处在于,unique_ptr 指针指向的堆内存无法同其它 unique_ptr 共享,也就是说,每个 unique_ptr 指针都独自拥有对其所指堆内存空间的所有权。
这也就意味着,每个 unique_ptr 指针指向的堆内存空间的引用计数,都只能为 1,一旦该 unique_ptr 指针放弃对所指堆内存空间的所有权,则该空间会被立即释放回收。
1.5.1 unique_ptr智能指针的创建
std::unique_ptr<int> p1();
std::unique_ptr<int> p2(nullptr);
std::unique_ptr<int> p3(new int);
和可以用 make_shared<T>() 模板函数初始化 shared_ptr 指针不同,C++11 标准中并没有为 unique_ptr 类型指针添加类似的模板函数。
std::unique_ptr<int> p4(new int);
std::unique_ptr<int> p5(p4);//错误,堆内存不共享
std::unique_ptr<int> p5(std::move(p4));//正确,调用移动构造函数
1.5.2 释放堆内存
默认情况下,unique_ptr 指针采用 std::default_delete 方法释放堆内存。当然,我们也可以自定义符合实际场景的释放规则。值得一提的是,和 shared_ptr 指针不同,为 unique_ptr 自定义释放规则,只能采用函数对象的方式。例如:
//自定义的释放规则
struct myDel
{
void operator()(int *p) {
delete p;
}
};
std::unique_ptr<int, myDel> p6(new int);
//std::unique_ptr<int, myDel> p6(new int, myDel());
1.5.3 成员方法
#include <iostream>
#include <memory>
using namespace std;
int main()
{
std::unique_ptr<int> p5(new int);
*p5 = 10;
// p 接收 p5 释放的堆内存
int * p = p5.release();
cout << *p << endl;
//判断 p5 是否为空指针
if (p5) {
cout << "p5 is not nullptr" << endl;
}
else {
cout << "p5 is nullptr" << endl;
}
std::unique_ptr<int> p6;
//p6 获取 p 的所有权
p6.reset(p);
cout << *p6 << endl;;
return 0;
}
1.6 weak_ptr
该类型指针通常不单独使用(没有实际用处),只能和 shared_ptr 类型指针搭配使用。
借助 weak_ptr 类型指针, 我们可以获取 shared_ptr 指针的一些状态信息,比如有多少指向相同的 shared_ptr 指针、shared_ptr 指针指向的堆内存是否已经被释放等等。
需要注意的是,当 weak_ptr 类型指针的指向和某一 shared_ptr 指针相同时,weak_ptr 指针并不会使所指堆内存的引用计数加 1;同样,当 weak_ptr 指针被释放时,之前所指堆内存的引用计数也不会因此而减 1。也就是说,weak_ptr 类型指针并不会影响所指堆内存空间的引用计数。
1.6.1 weak_ptr指针的创建
std::shared_ptr<int> sp (new int);
std::weak_ptr<int> wp3 (sp);
1.6.2 weak_ptr成员方法
#include <iostream>
#include <memory>
using namespace std;
int main()
{
std::shared_ptr<int> sp1(new int(10));
std::shared_ptr<int> sp2(sp1);
std::weak_ptr<int> wp(sp2);
//输出和 wp 同指向的 shared_ptr 类型指针的数量
cout << wp.use_count() << endl;
//释放 sp2
sp2.reset();
cout << wp.use_count() << endl;
//借助 lock() 函数,返回一个和 wp 同指向的 shared_ptr 类型指针,获取其存储的数据
cout << *(wp.lock()) << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
// 1.支持 -> 访问 Node 成员函数
// 2.支持 (*obj). 访问 Node 成员函数,obj 表示 SPNode对象
// 3.支持判断内部指针是否为空的判断:if (obj) {}
class Node
{
public:
Node(int age, std::string name) : nAge(age), strName(name) {}
void SetAge(int age) { nAge = age; }
int GetAge() { return nAge; }
void SetName(std::string name) { strName = name; }
std::string GetName() { return strName; }
private:
int nAge;
std::string strName;
};
class SPNode
{
public:
SPNode(Node *node)
{
m_pNode = node;
refCount = (unsigned*)malloc(sizeof(unsigned*));
*refCount = 1;
}
~SPNode()
{
(*refCount)--;
if (*refCount == 0)
{
delete m_pNode;
free(refCount);
m_pNode = NULL;
refCount = NULL;
}
}
Node& operator*(){
return *m_pNode;
}
Node* operator->(){
return m_pNode;
}
private:
Node *m_pNode;
unsigned *refCount; // 注意为什么是unsigned,还是指针
};
int main()
{
return 0;
}
2 练习
2.1 自定义智能指针
问题:请把下面代码片段中的 SPNode 实现成为智能指针类,并写一个简短的使用示例(3分)
要求:
1.支持 -> 访问 Node 成员函数
2.支持 (*obj). 访问 Node 成员函数,obj 表示 SPNode对象
3.支持判断内部指针是否为空的判断:if (obj) {}
class Node
{
public:
Node(int age, std::string name) : nAge(age), strName(name) {}
void SetAge(int age) { nAge = age; }
int GetAge() { return nAge; }
void SetName(std::string name) { strName = name; }
std::string GetName() { return strName; }
private:
int nAge;
std::string strName;
};
class SPNode
{
public:
SPNode(Node *node);
private:
Node *m_pNode;
};
解答:
#include <iostream>
#include <memory>
class Node
{
public:
Node(int age, std::string name) : nAge(age), strName(name) {}
void SetAge(int age) { nAge = age; }
int GetAge() { return nAge; }
void SetName(std::string name) { strName = name; }
std::string GetName() { return strName; }
private:
int nAge;
std::string strName;
};
class SPNode {
private:
Node* _ptr;
size_t* _count;
public:
SPNode(Node* ptr = nullptr) :
_ptr(ptr) {
if (_ptr) {
_count = new size_t(1);
} else {
_count = new size_t(0);
}
}
SPNode(const SPNode& ptr) {
if (this != &ptr) {
this->_ptr = ptr._ptr;
this->_count = ptr._count;
(*this->_count)++;
}
}
SPNode& operator=(const SPNode& ptr) {
if (this->_ptr == ptr._ptr) {
return *this;
}
if (this->_ptr) {
(*this->_count)--;
if (this->_count == 0) {
delete this->_ptr;
delete this->_count;
}
}
this->_ptr = ptr._ptr;
this->_count = ptr._count;
(*this->_count)++;
return *this;
}
Node& operator*() {
return *(this->_ptr);
}
Node* operator->() {
return this->_ptr;
}
~SPNode() {
(*this->_count)--;
if (*this->_count == 0) {
delete this->_ptr;
delete this->_count;
}
}
size_t use_count(){
return *this->_count;
}
bool isPtrNull()
{
if(_ptr == NULL) return true;
else return false;
}
};
int main()
{
SPNode sp1(new Node(13, "jack"));
SPNode sp2(new Node(15, "lucy"));
SPNode sp3(sp1);
std::cout<<"age2:"<<(*sp2).GetAge()<<" name2:"<<(*sp2).GetName()<<std::endl;
std::cout<<"age3:"<<sp3->GetAge()<<" name3:"<<sp3->GetName()<<std::endl;
std::cout<<sp1.use_count()<<std::endl;
std::cout<<sp2.use_count()<<std::endl;
}