C++ Primer 学习笔记——动态内存与智能指针(1)
2016-03-01 16:42
141 查看
程序用堆来存储动态分配的对象——即那些在程序运行时候分配的对象
当动态对象不再时候时,我们的代码必须显式地销毁他们
智能指针——shared_ptr
shared_ptr允许多个指针指向同一个对象
unique_ptr独占所指向的对象
weak_ptr是一种弱引用,指向shared_ptr所管理的对象
智能指针也是模板,在memory头文件中
make_shared函数
在动态内存中分配一个对象并初始化他,返回指向该对象的shared_ptr
shared_ptr自动销毁所管理的对象、自动释放相关联的内存
每个shared_ptr都有个计数器,无论何时拷贝一个shared_ptr,计数器会自加,而当我们给它赋一个新值(让他指向别的值),或者被销毁,计数器就自减。当计数器为0时,shared_ptr类自动销毁此对象(通过析构函数)
对于一块内存,shared_ptr类保证只要有任何shared_ptr对象引用他,他就不会被释放
直接管理内存 new和delete
除非使用智能指针,否则不要分配动态内存
new返回一个指向该对象的指针,如果分配失败,返回一个空指针
由内置指针管理的动态内存在被显示释放之前一直会存在
shared_ptr和new一起使用
智能指针——unique_ptr
在某一时候只能有一个unique_ptr指向指定对象
且不能拷贝和赋值(除了拷贝或赋值给一个即将被销毁的unique_ptr)
p.release();返回当前保存的指针并将其置空
p.reset();释放p指向的对象
unique_ptr需要delete来释放对象
智能指针——weak_ptr
指向一个由shared_ptr管理的对象,
//12.20
遇到了A类中需要用B类,B类中需要A类的问题
暂时没解决
当动态对象不再时候时,我们的代码必须显式地销毁他们
智能指针——shared_ptr
shared_ptr允许多个指针指向同一个对象
unique_ptr独占所指向的对象
weak_ptr是一种弱引用,指向shared_ptr所管理的对象
智能指针也是模板,在memory头文件中
shared_ptr<int> p; unique_ptr<string> p2; *p p->mem p.get() swap(p,q)
make_shared函数
在动态内存中分配一个对象并初始化他,返回指向该对象的shared_ptr
shared_ptr<string> p=make_shared<string>(); auto p2=make_shared<int>(23);
shared_ptr自动销毁所管理的对象、自动释放相关联的内存
每个shared_ptr都有个计数器,无论何时拷贝一个shared_ptr,计数器会自加,而当我们给它赋一个新值(让他指向别的值),或者被销毁,计数器就自减。当计数器为0时,shared_ptr类自动销毁此对象(通过析构函数)
对于一块内存,shared_ptr类保证只要有任何shared_ptr对象引用他,他就不会被释放
//定义StrBlob类,如果2个对象公用底层的数据,当某个对象被销毁时,我们不能单方面的消除底层数据 class StrBlob { public: typedef std::vector<string>::size_type size_type; StrBlob() :data(make_shared<vector<string>>()){}; StrBlob(std::initializer_list<string> il) : data(make_shared<vector<string>>(il)){}//参数数量不定的类 size_type size(){ return data->size(); } void push_back(const string &s) { data->push_back(s); } void pop_back() { check(0, "123"); data->pop_back(); } string& front(); string& back(); string& front() const; string& back() const; private: shared_ptr<vector<string>> data; void check(size_type i, const string &msg) const { if (i >= data->size()) throw out_of_range(msg); } }; string& StrBlob::front() { return data->front(); } string& StrBlob::back() { return data->back(); } string& StrBlob::front() const { return data->front(); } string& StrBlob::back() const { return data->back(); }
直接管理内存 new和delete
除非使用智能指针,否则不要分配动态内存
Elemtype *p=new Elemtype(initdata); auto p=new auto(obj); const int *p2=new const int(10); delete p; delete p2;
new返回一个指向该对象的指针,如果分配失败,返回一个空指针
由内置指针管理的动态内存在被显示释放之前一直会存在
//12.6使用内置指针 #include<iostream> #include<vector> using namespace std; vector<int>* f() { return new vector < int > ; } vector<int>* input() { auto T = f(); int word; while (cin >> word) T->push_back(word); return T; } void output(vector<int>* T) { auto begin = T->begin(); while (begin != T->end()) { cout << *begin; begin++; } delete T; } int main() { output(input()); return 0; } //12.7使用智能指针 #include<iostream> #include<vector> #include<memory> using namespace std; shared_ptr<vector<int>> f() { return make_shared < vector<int> >(); } shared_ptr<vector<int>> input() { auto p = f(); int word; while (cin >> word) { p->push_back(word); } return p; } void output(shared_ptr < vector<int>> T) { auto begin = T->begin(); while (begin != T->end()) { cout << *begin; begin++; } } int main() { output(input()); return 0; }
shared_ptr和new一起使用
shared_ptr<int> p(new int(4));//必须使用直接初始化
智能指针——unique_ptr
在某一时候只能有一个unique_ptr指向指定对象
且不能拷贝和赋值(除了拷贝或赋值给一个即将被销毁的unique_ptr)
unique_ptr<int> p(new int(2));//只能直接初始化
p.release();返回当前保存的指针并将其置空
p.reset();释放p指向的对象
unique_ptr需要delete来释放对象
智能指针——weak_ptr
指向一个由shared_ptr管理的对象,
weak_ptr<T> wp(sp);
//12.20
遇到了A类中需要用B类,B类中需要A类的问题
暂时没解决
相关文章推荐
- C++ primer 第五版 中文版 练习 10.37
- 二维数组回形遍历(北大信科2015上机考试)
- c语言的结构体详解
- c/c++在windows下获取时间和计算时间差的几种方法总结
- #if defined和#if !defined(c语言的宏定义)
- 2014第五届蓝桥杯C++B组第七题
- C语言中你必须知道的100个问题
- C语言实现单链表节点的删除(带头结点)
- 使用C++封装互斥锁的实践
- C++文件读写详解(ofstream,ifstream,fstream)
- C++内存管理
- C语言中switch语句的思考
- c++防止客户端多开巧妙代码
- C++ 虚函数表解析
- opencv error assertion failed,imread读到的总是空数据,Microsoft C++ 异常: 内存位置 0x0043f888 处的 cv::Exception。
- C语言中的存储类型
- 【C++11新特性】 C++11智能指针之weak_ptr
- C++ primer 第五版 中文版 练习 10.33
- 0.2 简单搭建C语言环境
- Java调用C/C++编写的第三方dll动态链接库(zz)