I'm in the process of porting my game engine from Java to C++. I've been doing a lot of research on custom memory allocators as I now need to worry about memory management. Every article that I've read on the topic says that you should overload the new
and delete
operators. However, these overloaded operators usually just delegate to and call a custom function that handles the memory allocation. Furthermore, macros are commonly used such as:
#define New(className, size, description) customNew(className, size, description, __FILE__, __LINE__)
which never even use the overloaded new
operator, and simply calls the custom method. I understand how the macros are useful as you can get the filename and line numbers nicely, but why overload new
and delete
if in code you're simply going to use the macro New
and not the operators.
Also, overriding (not overloading) the default new
seems somewhat pointless when our custom allocator may want to know additional information such as a description, a filename and line numbers. We can't possibly know that information on a standard invocation of new
and can't delegate it to our customNew
in a meaningful way.
I'm failing to see what the great benefit is of overloading and overriding these operators.
It seems to me that calling:
customNew(className, size, desc, file, line);
is more intuitive than calling the overloaded new
:
new (size, desc, file, line) className();
Finally, if I were to override and overload, where is the best place to do so? Doing it per class is obnoxiously tedious. Can it be done by namespace? I plan on having separate projects for each sub-system so that they can be maintained independently. How would I facilitate overriding and overloading across the projects and then within the game's project? Pardon my C++ n00bishness. It's been a while.