C++ 智能指针动态内存简单测试

代码示例,主要来自《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); // 释放,归还系统
    }
}

输出:

/*********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()*********/

按任意键关闭此窗口. . .
posted @ 2024-07-17 13:23  double64  阅读(2)  评论(0编辑  收藏  举报