Context: I'm working on a project where a client needs us to use custom dynamic memory allocation instead of allocating objects from the stack. Note that the objects in question have size known during compilation and doesn't even require dynamic allocation. Which makes me wonder,
What are some contexts where custom dynamic memory allocation of objects can be better than allocating objects from the stack? (where size is known during compilation)
An example. If Dog
is a class, then instead of just declaring Dog puppy;
they want us to do
Dog* puppy = nullptr;
custom_alloc(puppy);
new(puppy) Dog(); // the constructor
// do stuff
puppy->~Dog(); // the destructor
custom_free(puppy)
The real custom_alloc
function is not known to us. To make the program run, the given custom_alloc
function would be a wrapper of malloc
. And custom_free
would be a wrapper of free
I do not like this approach and was wondering when this can be actually useful or what they are really trying to solve by doing this.