《C++ Primer》第五版,第12章,主要是介绍了动态内存与智能指针方面的知识点。
C++ 11新标准,提供了两种智能指针类型shared_ptr和unique_ptr来管理动态内存。智能指针的行为类似常规指针,重要的区别是它负责自动释放所指向的对象。
我们平时创建动态内存及释放内存的方式,例如:
int *pt = new int(11);
delete pt;
使用智能指针的方式是:
shared_ptr<int> p = make_shared<int>(11);
shared_ptr允许多个指针指向同一个对象,即指向同一块内存。每个shared_ptr都有一个关联的计数器,通常称为引用计数。无论何时我们拷贝一个shared_ptr,计数器都会递增。例如,当用一个shared_ptr初始化另一个shared_ptr,或将它作为参数传递给一个函数以及作为函数返回值时,它所关联的计数器都会递增。当一个shared_ptr被销毁(比如离开所属的作用域),其计数器就会减1。一旦一个shared_ptr的计数器变为0,它就会自动释放自己所管理的对象。
智能指针shared_ptr和unique_ptr所支持的操作见下图:
使用动态内存的一个常见原因时允许多个对象共享相同的状态。书中定义了StrBlob类,引用了shared_ptr的功能,实现我们所希望的数据共享。我们在StrBlob设置一个shared_ptr来管理动态分配的vector。此shared_ptr的成员将记录有多少个StrBlob共享相同的vector,并在vector的最后一个使用者被销毁时释放vector。
StrBlob类代码如下,但比书中多增加了两个成员函数,printBlob()用于输出vector容器的内容,useCount()用于统计共享内存的引用计数。
// StrBlob.h
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <initializer_list>
#include <algorithm>
#include <memory>
using namespace std;
class StrBlob
{
public:
typedef vector<string>::size_type size_type;
StrBlob();
~StrBlob();
StrBlob(initializer_list<string> il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
// 添加和删除元素
void push_back(const string &t) { data->push_back(t); }
void pop_back();
// 元素访问
string &front();
string &front() const;
string &back();
string &back() const;
// 打印元素
void printBlob();
// 统计共享动态内存引用计数
unsigned long useCount();
private:
shared_ptr<vector<string>> data;
void check(size_type i, const string &msg) const;
};
// StrBlob.cpp
#include "StrBlob.h"
StrBlob::StrBlob() : data(make_shared<vector<string>>())
{
}
StrBlob::StrBlob(initializer_list<string> il) : data(make_shared<vector<string>>(il))
{
}
StrBlob::~StrBlob()
{
}
void StrBlob::check(size_type i, const string &msg) const
{
if (i >= data->size())
throw out_of_range(msg);
}
string& StrBlob::front()
{
check(0, "front on empty StrBlob");
return data->front();
}
string& StrBlob::front() const
{
check(0, "front on empty StrBlob");
return data->front();
}
string& StrBlob::back()
{
check(0, "back on empty StrBlob");
return data->front();
}
string& StrBlob::back() const
{
check(0, "back on empty StrBlob");
return data->front();
}
void StrBlob::pop_back()
{
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
void StrBlob::printBlob()
{
for_each(data->begin(), data->end(), [](const string& s) {cout << left << setw(12) << s;});
cout << endl;
}
// 统计共享动态内存的引用计数
unsigned long StrBlob::useCount()
{
return data.use_count();
}
mian()函数中的调用部分如下:
#include "StrBlob.h"
int main()
{
StrBlob blob1;
cout << blob1.size() << endl;
cout << blob1.useCount() << endl;
{
StrBlob blob2({"Hello", "Morning", "Best Wishes"});
blob1 = blob2;
blob2.push_back("Good Luck");
cout << blob1.size() << endl;
cout << blob1.useCount() << endl;
cout << blob2.useCount() << endl;
}
blob1.printBlob();
cout << blob1.useCount() << endl;
StrBlob blob3({"apple", "pear", "banana", "orange"});
blob3.printBlob();
cout << blob3.useCount() << endl;
return 0;
}
其允许结果如下: