[英]How should a C++ allocator handle its allocated memory when it is destroyed/copied/moved?
I am currently writing an allocator that should be usable by C++ standard data structures, ie, it implements the Allocator concept. 我目前正在编写一个应该由C ++标准数据结构使用的分配器,即,它实现了分配器的概念。
The allocator is quite simple: It allocates chunks of x
objects and always hands out the next object if the current chunk is not full, otherwise it allocates a new chunk. 分配器非常简单:它分配
x
对象的块,如果当前块未满,则始终分发下一个对象,否则分配一个新的块。
Now my question is: How to handle these chunks when the allocator is destroyed/copied/moved? 现在我的问题是:当分配器被销毁/复制/移动时,如何处理这些块? The allocator concept says nothing about what must happen in these cases.
分配器概念没有说明在这些情况下必须发生的情况。
Here are my thoughts: 这是我的想法:
Are my assumptions correct? 我的假设正确吗? If not, then why and where is this defined?
如果没有,那么为什么在何处定义?
Allocators usually are lightweight objects that can be copied around and destroyed, eg in the standard container classes. 分配器通常是轻量级的对象,可以在周围复制和销毁,例如在标准容器类中。 Therefore they should not do the heavy memory management themselves but relay it to some more permanent memory manager object.
因此,他们不应该自己执行繁重的内存管理,而应将其中继给其他一些永久性的内存管理器对象。 If you do that, the lifetime of the memory chunks does not depend on the allocator lifetimes but on the lifetime of the memory manager object.
如果这样做,则内存块的生存期不取决于分配程序的生存期,而是取决于内存管理器对象的生存期。 The lifetime thoughts therefore have to be applied to both types of objects:
因此,一生的想法必须应用于两种类型的对象:
Allocator (short lifetime): 分配器 (寿命短):
Memory manager (long lifetime): 内存管理器 (使用寿命长):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.