gpt4 book ai didi

visual-c++ - C++0x 分配器

转载 作者:行者123 更新时间:2023-12-04 06:42:29 26 4
gpt4 key购买 nike

我观察到我的 MSVC10 副本附带的容器似乎允许基于状态的分配器,并编写了一个简单的池分配器,为特定类型分配池。
然而,我发现如果_ITERATOR_DEBUG_LEVEL != 0 MSVC 向量从传递的分配器创建一个代理分配器(用于迭代器跟踪?),使用代理,然后让代理超出范围,期望分配的内存保留。这会导致问题,因为我的分配器试图在销毁时释放它的池。 C++0x 标准允许这样做吗?

代码大致是这样的:

class _Container_proxy{};

template<class T, class _Alloc>
class vector {
_Alloc _Alval;
public:
vector() {
// construct _Alloc<_Container_proxy> _Alproxy
typename _Alloc::template rebind<_Container_proxy>::other
_Alproxy(_Alval);
//allocate
this->_Myproxy = _Alproxy.allocate(1);
/*other stuff, but no deallocation*/
} //_Alproxy goes out of scope

~_Vector_val() { // destroy proxy
// construct _Alloc<_Container_proxy> _Alproxy
typename _Alloc::template rebind<_Container_proxy>::other
_Alproxy(_Alval);
/*stuff, but no allocation*/
_Alproxy.deallocate(this->_Myproxy, 1);
} //_Alproxy goes out of scope again

最佳答案

根据 17.6.3.5 节的分配器巨表要求,分配器必须是可复制的。容器可以自由复制它们。因此,您需要将池存储在 std::shared_ptr 中或类似的东西,以防止在分配器之一存在时删除。

关于visual-c++ - C++0x 分配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7250237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com