代码示例,主要来自《C++ Primer》,动态内存相关那章内容。

#include <iostream>
#include <memory>
#include <string>

namespace
{
    // 未初始化的智能指针,默认保存的空指针
    void def_null_sp_test();
    // 不是唯一用户,复制一份新的考拷贝。
    void sp_unique_copy_test();
    // weak_ptr 测试
    void wp_test();
    // 智能指针和动态数组
    void smartPtr_arr_test();
    // allocator 类测试
    void allocator_test();
}

int main()
{
    using namespace std;

    cout << "/*********def_null_sp_test()*********/" << endl;
    def_null_sp_test();
    cout << "/*********def_null_sp_test()*********/" << endl << endl;

    cout << "/*********sp_unique_copy_test()*********/" << endl;
    sp_unique_copy_test();
    cout << "/*********sp_unique_copy_test()*********/" << endl << endl;

    cout << "/*********wp_test()*********/" << endl;
    wp_test();
    cout << "/*********wp_test()*********/" << endl << endl;

    cout << "/*********smartPtr_arr_test()*********/" << endl;
    smartPtr_arr_test();
    cout << "/*********smartPtr_arr_test()*********/" << endl << endl;

    cout << "/*********allocator_test()*********/" << endl;
    allocator_test();
    cout << "/*********allocator_test()*********/" << endl << endl;

    return 0;
}

namespace
{
    void def_null_sp_test()
    {
        using namespace std;
        shared_ptr<string> p1;
        //cout << *p1 << endl; // p1 默认保存的是空指针

        if (!p1)
        {
            p1.reset(new string("hi, I'am jack."));
        }
        cout << *p1 << endl;

        *p1 = "";
        if (p1 && p1->empty())
        {
            *p1 = "hello world!";
        }
        cout << *p1 << endl;
    }

    void sp_unique_copy_test()
    {
        using namespace std;
        shared_ptr<string> p1(new string("hello world!"));
        shared_ptr<string> p2 = p1;
        shared_ptr<string> p3 = p2;
        cout << "p1 对象的引用次数,use_count = " << p1.use_count() << endl;

        cout << "p3 = " << *p3 << endl;
        if (!p3.unique())
        {
            p3.reset(new string(*p3));
        }
        *p3 += " good morning.";
        cout << "p3 新内容是:" << *p3 << endl;
        cout << "p1 对象的引用次数,use_count = " << p1.use_count() << endl;
        cout << "p1 的内容是:" << *p1 << endl;
    }

    void wp_test()
    {
        using namespace std;
        auto sp = make_shared<int>(100);
        weak_ptr<int> wp(sp);
        cout << "sp 的引用次数 use_count = " << sp.use_count() << endl;;
        auto ret = wp.lock();
        if (!ret)
        {
            cout << "对象已经被释放!" << endl;
        }
        else
        {
            cout << "sp 的引用次数 use_count = " << sp.use_count() << endl;
        }
    }

    void smartPtr_arr_test()
    {
        using namespace std;
        unique_ptr<int[]> up_arr(new int[10]); // 释放时自己会调用 delete[], 人肉经常会忘记[]
        for (size_t i = 0; i != 10; ++i)
        {// 赋值
            up_arr[i] = i * 10;
        }
        for (size_t i = 0; i != 10; ++i)
        {// 显示
            cout << "up_arr[i] = " << up_arr[i] << " ";
        }
        cout << endl;

        // 如果用 shared_ptr 管理动态数组,需要自定义删除器,记得带[]
        shared_ptr<int> sp_arr(new int[10], [](int* p) {delete[] p; });
        // 遍历只能这样
        for (size_t i = 0; i != 10; ++i)
        {
            *(sp_arr.get() + i) = i * 10;
        }
        for (size_t i = 0; i != 10; ++i)
        {
            cout << "sp_arr[i] = " << *(sp_arr.get() + i) << " ";
        }
        cout << endl;
    }

    void allocator_test()
    {
        using namespace std;
        const size_t ALLOC_SIZE = 3;

        allocator<string> allco_strs;
        auto const p = allco_strs.allocate(ALLOC_SIZE); // 分配n个未初始化的string

        auto q = p;
        allco_strs.construct(q++); // 空字符串
        allco_strs.construct(q++, 5, 'c');
        allco_strs.construct(q++, "hello world");

        cout << *p << endl;

        cout << "allocator 遍历:" << endl;
        while (q != p)
        {
            auto temp = --q;
            cout << *temp << endl;
            allco_strs.destroy(temp); // 析构
        }

        allco_strs.deallocate(p, ALLOC_SIZE); // 释放,归还系统
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.

输出:

/*********def_null_sp_test()*********/
hi, I'am jack.
hello world!
/*********def_null_sp_test()*********/

/*********sp_unique_copy_test()*********/
p1 对象的引用次数,use_count = 3
p3 = hello world!
p3 新内容是:hello world! good morning.
p1 对象的引用次数,use_count = 2
p1 的内容是:hello world!
/*********sp_unique_copy_test()*********/

/*********wp_test()*********/
sp 的引用次数 use_count = 1
sp 的引用次数 use_count = 2
/*********wp_test()*********/

/*********smartPtr_arr_test()*********/
up_arr[i] = 0 up_arr[i] = 10 up_arr[i] = 20 up_arr[i] = 30 up_arr[i] = 40 up_arr[i] = 50 up_arr[i] = 60 up_arr[i] = 70 up_arr[i] = 80 up_arr[i] = 90
sp_arr[i] = 0 sp_arr[i] = 10 sp_arr[i] = 20 sp_arr[i] = 30 sp_arr[i] = 40 sp_arr[i] = 50 sp_arr[i] = 60 sp_arr[i] = 70 sp_arr[i] = 80 sp_arr[i] = 90
/*********smartPtr_arr_test()*********/

/*********allocator_test()*********/

allocator 遍历:
hello world
ccccc

/*********allocator_test()*********/

按任意键关闭此窗口. . .
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.