【发布时间】:2014-09-08 07:38:21
【问题描述】:
我对某事感到困惑。假设我有一个任意的 C++ 分配器——比如说,像这样:
template<class T>
struct my_allocator
{
template<class Other>
struct rebind { typedef my_allocator<Other> other; };
// [other members here]
};
现在考虑以下代码(请阅读 cmets):
typedef my_allocator<int> Alloc;
Alloc alloc = get_my_allocator(); // assume this works properly
long *const p = Alloc::rebind<long>::other(alloc).allocate(1, NULL);
// Notice that the rebound allocator for 'long' is now destroyed
// Can a NEW rebound allocator for 'long' deallocate the memory from the old one?
Alloc::rebind<long>::other(alloc).deallocate(p, 1);
// i.e., does the 'int' allocator 'alloc' keep alive the 'long' memory pool too?
究竟在什么时候可以释放后备存储池?
或者,换句话说:哪个分配器共享哪个内存池的所有权?
我一直认为 - 没有太多考虑 - 相同值类型的分配器共享他们自己的内存池的所有权,但现在我想到了他们也可以共享所有 rebound 分配器后面的内存池的所有权,即使它们管理完全不同的类型。
反弹类型的分配器必须“保持活跃”彼此的内存池直到所有它们都被销毁?
如果 C++03 和 C++11 的答案不同,请说明两者以及它们之间的区别。
【问题讨论】:
-
@Downvoter:我希望上帝知道当你点击那个按钮时你在想什么,因为我肯定不知道!
-
您在谈论没有上下文的内存池和分配器。它肯定超出了我的想象。只是一点点额外的背景可能会使更广泛的受众受益。 PS。我不是反对者。
标签: c++ c++11 memory-management allocator