I’m writing a stack based allocator for STL. The idea is to allocate a big chunk of memory once and let it build up while objects are constructed and then reset it to zero when needed. For example, this could be used for storing level information for a game or for objects that are created for a single iteration of a game loop. When you load a level you build up the stack, when you need to load a new a level you just reset the top pointer to the beginning, no OS calls needed.
Now my issue is the way the standard allocator does things, namely the deallocate() function. In this article i can read that
In the default allocator, the block of storage is deallocated using
::operator delete.
which implies that by using the deallocate() function both the memory is freed and the destructor is called. Now for the purpouse of my allocator i can make the deallocate() function to be empty, since i’m not going to deallocate memory for individual objects.
So, the question is, how do the STL containers use the allocator class to create new objects? Since the default allocator uses new and delete, do the containers ever call construct() or destroy()? Should i make my deallocate() function also call the descrutor?
I guess the same question goes for the allocate() and construct() methods.
No it doesn’t.
::operator deleteis the global delete function, it just frees the memory. Thedeleteoperator is a different thing, despite the similarity of name — it destructs and then calls a delete function (either the global delete function or an overload).The destructor is called by the
destroyfunction of the allocator.Standard containers use
allocateto get some memory, thenconstructif and when they need that memory to contain an element,destroyif and only if theyconstructed, anddeallocateto free the memory. For example: