smart pointer

Following examples of book <The C++ Standard Template Library: a Tutorial and Reference>.
#include<iostream>
#include<string>
#include<vector>
#include<memory>
using namespace std;

int main(){
    shared_ptr<string> pNico(new string("nico"));
    shared_ptr<string> pJutta(new string("jutta"));

    (*pNico)[0] = 'N';
    pJutta->replace(0, 1, "J");

    vector<shared_ptr<string>> whoMadeCoffee;
    whoMadeCoffee.push_back(pNico);
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pNico);

    auto itr = whoMadeCoffee.begin();
    cout << "Before change: " << endl;
    while(itr!=whoMadeCoffee.end()){
        cout << **itr << endl;
        itr++;
    }

    *pNico = "Nicolai";
    itr = whoMadeCoffee.begin();
    cout << "After change: " << endl;
    while(itr!=whoMadeCoffee.end()){
        cout << **itr << endl;
        itr++;
    }
    
    cout << "use_cont: " << whoMadeCoffee[0].use_count() << endl;

    return 0;
}

Use user defined destructor.

#include<iostream>
#include<string>
#include<vector>
#include<memory>
using namespace std;

int main(){
    shared_ptr<string> pNico(new string("nico"), [](string* p){cout<<"delete "<<*p<<endl; delete p;});
    shared_ptr<string> pJutta(new string("jutta"));

    (*pNico)[0] = 'N';
    pJutta->replace(0, 1, "J");

    vector<shared_ptr<string>> whoMadeCoffee;
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pNico);
    whoMadeCoffee.push_back(pNico);

    auto itr = whoMadeCoffee.begin();
    cout << "Before change: " << endl;
    while(itr!=whoMadeCoffee.end()){
        cout << **itr << endl;
        itr++;
    }

    *pNico = "Nicolai";
    itr = whoMadeCoffee.begin();
    cout << "After change: " << endl;
    while(itr!=whoMadeCoffee.end()){
        cout << **itr << endl;
        itr++;
    }

    cout << "use_cont: " << whoMadeCoffee[0].use_count() << endl;
    whoMadeCoffee.resize(2);
    pNico = nullptr;

    return 0;
}
When deal with array, since the default deleter provided by shared_ptr calls delete, not delete[].  This means that the default deleter is appropriate only if a shared pointer owns a single object created with new.  If you use new[] to create an array of objects you have to define your own deleter. You can do that by passing a function, function object, or lambda, which calls delete[ ] for the passed ordinary pointer.
std::shared_ptr<int> p(new int[10], [](int* p){delete[] p;});


智能指针是C++中用于管理动态分配的内存的一种机制。它们可以自动地在不再需要时释放内存,从而避免内存泄漏和悬挂指针的问题。 shared_ptr是一种引用计数智能指针,它可以跟踪有多少个shared_ptr指向同一个对象,并在没有引用时自动释放内存。当创建shared_ptr时,它会增加引用计数,当销毁或重置shared_ptr时,它会减少引用计数。只有当引用计数为0时,才会真正释放内存。\[1\]shared_ptr可以通过构造函数接受一个指向动态分配对象的指针来创建,也可以使用std::make_shared函数来创建。\[2\] unique_ptr是一种独占智能指针,它拥有对动态分配对象的唯一所有权。当unique_ptr被销毁时,它会自动释放内存。unique_ptr不能被复制,但可以通过std::move函数进行转移所有权。\[3\]unique_ptr可以通过构造函数接受一个指向动态分配对象的指针来创建。 weak_ptr是一种弱引用智能指针,它指向由shared_ptr管理的对象,但不会增加引用计数。weak_ptr可以用于解决shared_ptr的循环引用问题,因为它不会导致对象无法释放。\[1\]weak_ptr可以通过shared_ptr的构造函数来创建。 auto_ptr是C++11之前的一种智能指针,它类似于unique_ptr,但有一些限制和问题。auto_ptr在复制时会转移所有权,这可能导致悬挂指针的问题。因此,auto_ptr已经被unique_ptr取代,不推荐使用。 总结来说,shared_ptr是引用计数智能指针,unique_ptr是独占智能指针,weak_ptr是弱引用智能指针,而auto_ptr是已经过时的智能指针。它们各自有不同的用途和特点,可以根据具体的需求选择使用。 #### 引用[.reference_title] - *1* *2* *3* [C++11 解决内存泄露问题的智能指针:shared_ptr、unique_ptr、weak_ptr](https://blog.csdn.net/weixin_44120785/article/details/128714630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值