分配器allocator和new重载

3 分配器allocator和new重载

3.3 分配器allocator详解

分配器allocator实现容器算法时不同于new,它可以将对象内存分配和构造分离。
allocator的使用如下所示:

# include <iostream>
using namespace std;
class XData {
    public:
    XData() 
    {
        cout << "construct function XData()" << endl;
    }
    ~XData()
    {
        cout << "destory function ~XData()" << endl;
    }
};

int main()
{
    /**
     * @brief 
     * #include <memory>
     * std::allocator
     * address:C++17 弃用, C++20中移除
     * max_size:C++17 弃用, C++20中移除
     * construct:C++17 弃用, C++20中移除
     * destory:C++17 弃用, C++20中移除
     * allocator:分配未初始化的存储
     * deallocator:解分配存储
     */
    allocator<XData> xdata_alloc;
    int size = 2;
    // 分配内存空间,不分配内存
    auto dataArr = xdata_alloc.allocate(size);
    for (size_t i = 0; i < size; i++)
    {
        // allocator_traits 类模板提供访问分配器(Allocator)的各种属性的标准化方式。
        // 调用构造函数
        allocator_traits<decltype(xdata_alloc)>::construct(xdata_alloc, &dataArr[i]);
        // 调用析构函数
        allocator_traits<decltype(xdata_alloc)>::destroy(xdata_alloc, &dataArr[i]);
    }

    // 清理空间,不调用析构
    xdata_alloc.deallocate(dataArr, size);

    return 0;
}

3.4 自定义allocator

可以实现内存共享、内存泄漏探测,预分配对象存储和内存池
演示自定义vector和list分配器

#include <iostream>
#include <vector>
using namespace std;
class XData {
public:
    XData() 
    {
        cout << "construct function XData(), index = " << index << endl;
    }

    XData(const XData &b) 
    {   
        this->index = b.index;
        cout << "copy function XData(), index = " << index << endl;
    }

    ~XData()
    {
        cout << "destory function ~XData()" << endl;
    }
    int index = 0;
};

template<typename Ty>
class MyAllocator {
public:
    using value_type = Ty;
    MyAllocator()=default;
    template<class other>
    MyAllocator(const MyAllocator<other> &) {};

    void deallocate(Ty *const ptr, const size_t count)
    {
        free(ptr);
    }
    Ty *allocate(size_t count)
    {
        cout << "allocate " << count << endl;
        return static_cast<Ty*>(malloc(sizeof(Ty) * count));
    }
};

int main()
{
    vector<XData, MyAllocator<XData>> vec;
    XData d;
    d.index = 1;
    vec.push_back(d);
    d.index = 2;
    vec.push_back(d);
    d.index = 3;
    vec.push_back(d);

    return 0;
}

输出结果:

construct function XData(), index = 0
allocate 1
copy function XData(), index = 1
allocate 2
copy function XData(), index = 2
copy function XData(), index = 1
destory function ~XData()
allocate 4
copy function XData(), index = 3
copy function XData(), index = 1
copy function XData(), index = 2
destory function ~XData()
destory function ~XData()
destory function ~XData()
destory function ~XData()
destory function ~XData()
destory function ~XData()

3.5 未初始化内存复制分析

#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
class XData {
public:
    XData() 
    {
        cout << "construct function XData(), index = " << index << endl;
    }
    XData(const XData &b) 
    {   
        this->index = b.index;
        cout << "copy function XData(), index = " << index << endl;
    }
    XData& operator=(const XData &d)
    {
        this->index = d.index;
        cout << "operator= function XData(), index = " << index << endl;
        return *this;
    }
    ~XData()
    {
        cout << "destory function ~XData()" << endl;
    }
    int index = 0;
};

template<typename Ty>
class MyAllocator {
public:
    using value_type = Ty;
    MyAllocator()=default;
    template<class other>
    MyAllocator(const MyAllocator<other> &) {};
    void deallocate(Ty *const ptr, const size_t count)
    {
        free(ptr);
    }
    Ty *allocate(size_t count)
    {
        cout << "allocate " << count << endl;
        return static_cast<Ty*>(malloc(sizeof(Ty) * count));
    }

};

int main()
{
    unsigned char buf[1024] = {0};
    XData datas[3];
    cout << "===============memcpy==================" << endl;
    memcpy(buf, &datas, sizeof(datas));
    cout << "===============std::copy==================" << endl;
    // 将对象进行赋值操作,对象没有做初始化
    copy(begin(datas), end(datas), reinterpret_cast<XData*>(buf));
    cout << "===============uninitialized_copy==================" << endl;
    // 将对象进行拷贝构造操作,对象做了初始化
    uninitialized_copy(begin(datas), end(datas), reinterpret_cast<XData*>(buf));
    return 0;
}

输出结果:

construct function XData(), index = 0
construct function XData(), index = 0
construct function XData(), index = 0
===============memcpy==================
===============std::copy==================
operator= function XData(), index = 0
operator= function XData(), index = 0
operator= function XData(), index = 0
===============uninitialized_copy==================
copy function XData(), index = 0
copy function XData(), index = 0
copy function XData(), index = 0
destory function ~XData()
destory function ~XData()
destory function ~XData()
#include <iostream>
#include <vector>
#include <string.h>
#include <memory>
using namespace std;
class XData {
public:
    XData() 
    {
        cout << "construct function XData(), index = " << index << endl;
    }
    XData(const XData &b) 
    {   
        this->index = b.index;
        cout << "copy function XData(), index = " << index << endl;
    }
    XData& operator=(const XData &d)
    {
        this->index = d.index;
        cout << "operator= function XData(), index = " << index << endl;
        return *this;
    }
    ~XData()
    {
        cout << "destory function ~XData()" << endl;
    }
    int index = 0;
};

template<typename Ty>
class MyAllocator {
public:
    using value_type = Ty;
    MyAllocator()=default;
    template<class other>
    MyAllocator(const MyAllocator<other> &) {};
    void deallocate(Ty *const ptr, const size_t count)
    {
        free(ptr);
    }
    Ty *allocate(size_t count)
    {
        cout << "allocate " << count << endl;
        return static_cast<Ty*>(malloc(sizeof(Ty) * count));
    }
};

int main()
{
    // C++ 17 20
    // construct_at destory对象构造和销毁
    // C++17 destory对象的销毁
    // C++20 construct_at对象构造
    int size = 3;
    // 分配内存
    auto data = static_cast<XData*>(malloc(sizeof(XData) * size));
    for (size_t i = 0; i < size; i++)
    {
        if (data) {
            std::construct_at(&data[i]); // 调用构造函数,构造对象
        }
    }
    // 调用析构函数
    destory(data, data + size);
    free(data);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值