C++Primer 学习笔记 第十二章 动态内存

//12.1 动态内存与智能指针
//在C++中,动态内存的管理是通过一对运算符来完成的:new,在动态内存中为对象分配空间并返回一个只想该对象的指针,我们可以选择对对象进行
//初始化;delete,接受一个动态对象的指针,销毁该对象,并释放与之关联的内存。

//动态内存的使用很容易出现问题,因为确保在正确的时间释放内存是极其困难的。有时我们会忘记释放内存,在这种情况下就会产生内存泄露;
//有时在尚有指针引用内存的情况下我们就释放了它,在这种情况下就会产生引用非法内存的指针。

//为了更容易地使用动态内存,新的标准库提供了两种智能指针来管理动态对象。智能指针地行为类似常规指针,重要的区别是它负责自动释放所指向地对象。
//新标准库提供地这两种智能指针地区别在于管理底层指针地方式:shared_ptr 允许多个指针指向同一个对象;unique_ptr则独占所指向的对象。
//标准库还定义了weak_ptr的伴随类,它是一种弱引用,指向shared_ptr所管理的对象。这三种类型都定义在memory头文件中。


//12.1.1 shared_ptr类
//类似vector,智能指针也是模板。


#include "iostream"
#include "string"
#include "list"
#include "memory"
#include "vector"
using namespace std;




//使用智能指针
int main1201(){ 
	//定义智能指针
	shared_ptr<string> p1;//shared_ptr,可以指向string
	shared_ptr<list<int>> p2;//shared_ptr,可以指向int的list
	if (p1 && p1->empty())
	{
		*p1 = "hi";
	}
	cout << *p1;
	system("pause");
	return 0;
}

//make_shared函数
//最安全的分配何使用动态内存的方法是调用一个名为make_shared的标准库函数。此函数在动态内存中分配一个对象并初始化它,返回指向此对象的shared_ptr。
//与智能指针一样,make_shared也定义在头文件memory中。

int main1202() {
	//指向一个值为42的int的shared_ptr
	shared_ptr<int> p3 = make_shared<int>(42);
	cout << *p3 << endl;
	//指向一个值为“999999999”的string
	shared_ptr<string> p4 = make_shared<string>(10, '9');
	cout << *p4 << endl;
	//指向一个值初始化的int,即值为0;
	shared_ptr<int> p5 = make_shared<int>();
	cout << *p5 << endl;
	//通常使用auto比较简单
	auto p6 = make_shared<int>(3);
	cout << *p6 << endl;
	system("pause");
	return 0;
}

class strBlob
{
public:
	typedef vector<string>::size_type size_type;
	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 &back();
protected:
private:
	shared_ptr<vector<string>> data;
	void check(size_type i, const string &msg) const;
};


//构造函数
strBlob::strBlob() :data(make_shared<vector<string>>()) {};
strBlob::strBlob(initializer_list<string> il) :data(make_shared<vector<string>>(il)) {};


//元素访问成员函数
void strBlob::check(size_type i, const string &msg) const {
	if (i>=data->size())
	{
		throw out_of_range(msg);
	}
}

string & strBlob::front() {
	//如果vector为空,check会抛出一个异常
	check(0, "front on empty strBlob");
	return data->front();
}

string & strBlob::back() {
	//如果vector为空,check会抛出一个异常
	check(0, "back on empty strBlob");
	return data->back();
}

void strBlob::pop_back() {
	//如果vector为空,check会抛出一个异常
	check(0, "pop_back on empty strBlob");
	 data->pop_back();
}


//12.1.2 直接管理内存
//C++语言定义了两个运算符来分配和释放动态内存
//new 分配内存,delete释放new分配的内存

//相对于智能指针,使用这两个运算符管理内存非常容易出错,随着我们逐步详细介绍这两个运算符,这一点会更为清楚。而且,自己管理内存的类与使用智能指针的类不同,
//他们不能依赖类对象拷贝、赋值和销毁操作的任何默认定义。因此,使用智能指针的程序更容易编写和调试。

int main1203() {
	int *pi = new int;//pi指向一个动态分配的,未初始化的无名对象。
	string *ps = new string;//初始化为空string
	//测试内存耗尽
	int count = 1;
	while (1)
	{
		new int[10000000];
		cout << ++count;
	}
	system("pause");
	return 0;
}


int main1104() {
	//动态分配的const对象
	const int *pci = new const int(1024);
	const string *pcs = new const string;

	//指针值和delete
	//我们传递给delete的指针必须指向动态分配的内存,或者是一个空指针。释放一块并非new分配的内存,或者将相同的指针值释放多次,其行为是未定义的:
	int i, *pil = &i, *pi2 = nullptr;
	//delete i; 错误
	//delete pil 错误

	//动态对象的生存期知道被释放时为止。
}

//小心:动态内存的管理非常容易出错
//1.忘记delete内存。
//2.使用已经释放掉的对象。
//3.同一块内存释放两次。

//解决方法,坚持使用智能指针。

//12.1.4 智能指针和异常
//如果使用智能指针,即使程序块过早结束,智能指针类也能确保在内存不再需要时将其释放:

void f() {
	shared_ptr<int> sp(new int(42));//分配一个新对象
	//这段代码抛出一个异常,且在f中未被捕获
}//在函数结束时shared_ptr自动释放内存

void f1() {
	int *ip = new int(42);//动态分配一个新对象
	//这段代码跑相互一个异常,且在f中未被捕获
	delete ip;//在退出之前释放内存。
}

//12.1.5 unique_ptr
//一个unique_ptr"拥有"它所指向的对象。当unique_ptr被销毁时,它所指向的对象也被销毁。
int main1204() {
	//初始化
	unique_ptr<double> p1;
	unique_ptr<int> p2(new int(42));

	//由于unique_ptr拥有它指向的对象,因此unique_ptr不支持普通的拷贝或赋值操作:
	unique_ptr<string> p1(new string("stegosaurus"));
	//unique_ptr<string> p2(p1);//错误,不支持拷贝
	//unique_ptr<string> p3;
	//p3=p2         //错误,不支持赋值
}

//传递unique_ptr参数和返回unique_ptr
//不能拷贝unique_ptr的规则有一个例外,我们可以拷贝或赋值要给将要被销毁的unique_ptr。最常见的例子就是函数返回一个unique_ptr。

//向unique_ptr传递删除器
//类似 shared_ptr, unique_ptr默认情况下使用delete释放它所指向的对象。


//12.1.6weak_ptr
//weak_ptr是一种不受控制所指向对象生存期的智能指针,它指向有一个shared_ptr管理的对象。将一个weak_ptr绑定到一个shared_ptr不会改变shared_ptr的引用计数。
//一旦,最后一个指向对象的shared_ptr被销毁,对象就会被释放。即使有weak_ptr指向对象,对象还是会被释放,因此,weak_ptr的名字抓住了这种智能指针“弱”共享对象的特点


//12.2动态数组
//12.2.1new和数组
//为了让new分配一个对象数组,我们要在类型名之后跟一对方括号,在其中要知名分配对象的数目。

//初始化动态分配对象的数组
/*
int *pia=new int [10];
int *pia2=new int [10]();
string *psa=new string[10];
string *psa2=new string[10]();
*/

//释放动态数组
/*
为了释放动态数组,我们使用一种特殊形式的delete,在指针前加上一个空方括号
delete p;
delete [] pa;

*/


//智能指针和动态数组

//12.2.2 allocator类
//allocator类
//标准库allocator类定义在头文件memory中,它帮助我们将内存分配和对象构造分离开来。

//#include "memory"
//allocator<string> alloc;//可以分配string的allocator对象
//int n;
//auto const p = alloc.allocate(n);//分配n个未初始化的string


//allocator分配未构造的内存
//allocator分配的内存是未构造的。我们按需要在此内存中构造对象。
//在新标准库中,construct 成员函数接受一个指针和零个或多个额外参数,在给定位置构造一个元素,额外参数用来初始化构造的对象。
//类似make_shared的参数(参见12.1.1节,第401页),这些额外的参数必须是与构造的对象的类型相匹配的合法的初始化器:
//auto q = p;//q指向最后构造的元素之后的位置
//alloc.construct(q++); //*q未空字符串
//alloc.construct(q++, 10, 'c');//*q为cccccccccc
//alloc.construct(q++, "hi");   //*q为hi!

//12.3使用标准库:文本查询程序
//我们将实现一个简单的文本巴哈寻程序,作为标准库相关内容学习的总结。我们的程序允许用户在一个给定文件中查询单词。
//查询结果是单词在文件中出现的次数及其所在行的列表。
//如果一个单词在一行中出现多次,此行只列出一次。行回按照升序输出——即,第一行会在第9行之前显示,以此类推。
#include "map"
#include "set"
#include "iostream"
#include "string"
#include "algorithm"
#include "fstream"
#include "memory"
#include<sstream>
using namespace std;
using line_no = std::vector<std::string>::size_type;
class QueryResult;//为了定义函数query的返回类型,这个定义是必须的
class TextQuery
{
public:
	
	TextQuery(std::ifstream&);
	QueryResult query(const std::string&) const;
protected:
private:
	std::shared_ptr < std::vector<std::string>> file;//输入文件
	std::map<std::string, std::shared_ptr<std::set<line_no>>> wm;
};

//TextQuery构造函数
TextQuery::TextQuery(ifstream &is) :file(new vector<string>) {
	string text;
	while (getline(is, text))
	{
		file->push_back(text);
		int n = file->size() - 1;
		istringstream line(text);     //将行文本分解为单词
		string word;
		while (line>>word)
		{
			auto &lines = wm[word];
			if (!lines)
			{
				lines.reset(new set<line_no>);
			}
			lines->insert(n);
		}
	}
}

//QueryResult类
class QueryResult
{
	friend std::ostream& print(ostream &, const QueryResult &);
public:
	QueryResult(string s, std::shared_ptr <set<line_no>> p, shared_ptr<vector<string>> f) :sought(s), lines(p), file(f) {};
protected:
private:
	string sought;
	shared_ptr<std::set<line_no>> lines;
	shared_ptr<vector<string>> file;
};

QueryResult
TextQuery::query(const string &sought) const {
	static shared_ptr<set<line_no>> nodata(new set<line_no>);
	auto loc = wm.find(sought);
	if (loc==wm.end())
	{
		return QueryResult(sought, nodata, file);
	}
	else
	{
		return QueryResult(sought, loc->second, file);
	}
}

//打印结果

string make_plural(size_t ctr, const string &word, const string &ending) {
	return (ctr > 1) ? word + ending : word;
}

ostream &print(ostream&os, const QueryResult &qr) {
	//如果找到了单词,打印出现次数和所有出现的的位置
	os << qr.sought << "occurs " << qr.lines->size() << " " << make_plural(qr.lines->size(), "times", "s") << endl;
	for (auto num: *qr.lines)
	{
		//避免行号从0开始给用户带来的困惑
		os << "\t(line" << num + 1 << ") " << *(qr.file->begin() + num) << endl;
	}
	return os;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值