I am currently writing an allocator that should be usable by C++ standard data structures, ie, it implements the Allocator concept.
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.
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):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.