Using a std::map as in the line marked with /* X */ below crashes the code during execution. If this line is erased, the code runs but creates the following unexpected output:
When you evaluate m[0] = 3; within f(), std::map creates a tree node with the key 0 and with value 3.
That node is allocated by calling new (unless you provide a non-default allocator to std::map at line 6).
That call to new calls m[0] again, which crashes your program, or, with that line commented out, executes a=5, which you then see in the output at line 21.
In line 16, you insert a new element into the map, for which it tries to allocate memory using new.
Your new operator also tries to insert the same element every time, thus entering infinite recursion.
Edit: too slow.
Agreed. I bet the OP is wondering why. Here's why:
Any classes or functions that rely on the global operator new (such as std::vector<T>) are going to fail. If you insist on overloading the global operator new, then ensure that it doesn't interfere with the allocations of dependant classes and functions. Alternatively, consider module programming. This incorporates the use of namespaces:
This will force you to qualify your version of operator new with Allocator. Of course, using namespace Allocator will break whole idea, and you'll be back at square 1.