title: Can the default C++ std::allocator be supplanted/superseded for 3rd party libraries?
body: <p>My team is working on an application, where we need to track memory usage, and provide statistics on how much memory areas of the program utilize (e.g. N bytes used by uncontrolled STL containers).  I need to find a way to identify memory allocated in 3rd party libs from STL containers.</p>
  
  <p>The application makes use of 3rd party libraries that either we don't have access to the source code, or have been directed not to make changes to the source.  Some of these libraries use standard STL containers, <code>like std::vector&lt;int&gt;</code>, but they have used (or appear to use, in the case of the closed libs) the default <code>std::allocator</code>.  We are targeting Windows, with future work planned for Mac and Linux platforms, using C++17 as much as possible.</p>
  
  <p>I've overridden the <code>malloc</code> and <code>free</code> functions; overridden <code>new</code>, <code>new[]</code>, <code>delete</code> and <code>delete[]</code> operators; and created an <code>STLAllocator</code> class derived from <code>std::allocator</code> that is used as the <code>_Alloc</code> template parameter for our use of STL containers.  For the libraries that provide hooks to replace the memory allocators, I have done so.  When the STL containers in the remaining 3rd partly libs use the default <code>std::allocator</code>, I can see their <code>new</code> and <code>delete</code> calls come through the <code>new</code> and <code>delete</code> overrides, but these appear no different to tracking than a call to <code>new</code> or <code>delete</code> made from <code>main</code>.</p>
  
  <p>I've read many great descriptions of how to declare and use your own <code>std::allocator</code> class, been reminded of the template parameter equality issue when providing different allocators, and made aware of an upcoming solution using <code>std::experimental::pmr::polymorphic_allocator</code>, but I haven't found a definitive answer to my question. Is there a way to supplant the default <code>std::allocator</code> for 3rd party libs that don't provide a hook to override the default <code>std::allocator</code> used by STL containers?</p>
  
  <p>For anyone interested, here is the link that describes the template parameter equality issue; it's also a good overview of std::allocator in general:
  <a href="https://blog.feabhas.com/2019/03/thanks-for-the-memory-allocator/" rel="nofollow noreferrer">https://blog.feabhas.com/2019/03/thanks-for-the-memory-allocator/</a></p>