C++ Primer 学习笔记——动态内存与智能指针(1)
来源:互联网 发布:页游挂机软件 编辑:程序博客网 时间:2024/06/06 09:26
程序用堆来存储动态分配的对象——即那些在程序运行时候分配的对象
当动态对象不再时候时,我们的代码必须显式地销毁他们
智能指针——shared_ptr
shared_ptr允许多个指针指向同一个对象
unique_ptr独占所指向的对象
weak_ptr是一种弱引用,指向shared_ptr所管理的对象
智能指针也是模板,在memory头文件中
shared_ptr<int> p;unique_ptr<string> p2;*pp->memp.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学习:智能指针与动态内存(1)
- C++ Primer 学习笔记——动态内存与智能指针(1)
- C++Primer学习:智能指针与动态内存(2)
- C++Primer学习:动态内存与智能指针(3)
- c++学习笔记—动态内存与智能指针浅析
- C++ Primer 学习笔记——动态内存和智能指针(2)
- 【足迹C++primer】39、动态内存与智能指针(1)
- C++primer学习笔记----智能指针
- c++ primer(第五版)学习笔记及习题答案代码版(第十二章)动态内存与智能指针
- C++ Primer笔记8_动态内存_智能指针
- 【足迹C++primer】39、动态内存与智能指针(2)
- 【足迹C++primer】39、动态内存与智能指针(3)
- 【C++ Primer 第5版 笔记】第12章 动态内存与智能指针
- 《C++ Primer》读书笔记第十二章-1-动态内存与智能指针
- 《C++Primer》读书笔记——第12章 动态指针与内存管理
- 动态内存与智能指针
- 动态内存与智能指针
- 动态内存与智能指针
- SPOJ 16549 QTREE6 - Query on a tree VI
- CSS 外边距(margin)重叠及防止方法
- ListView小知识点总结
- iOS中常用到的全局变量
- Android源码阅读工具——在Ubuntu下使用Source Insight
- C++ Primer 学习笔记——动态内存与智能指针(1)
- PL/SQL编程
- 在Windows平台上搭建Apache Storm环境
- 腾讯2016研发工程师编程题
- iOS多线程的初步研究(一、二、三)-- NSThread -- 锁-- NSRunLoop
- 第五届_蚂蚁感冒
- 跨境电商将迎税改新政
- Android文件操作(SD卡、缓存)工具类
- ajaxFileUpload json数据对象传到前台,IE OK,但是火狐却变成了文件下载问题