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类的问题
暂时没解决

0 0
原创粉丝点击