title: Globally override malloc in visual c++
body: <p>I'm trying to figure out a way to globally override malloc and related functions in visual c++ (2005). My setup is a dll with statically linked runtime library that consists of both my own c++ code, external c++ and c code. What I want to accomplish is to allow a user of the dll to set their own implementations of the memory allocation functions.</p>
  <p>Solutions that I can't use:</p>
  <ul>
  <li>Overriding new and delete globally, there is lots of external C libraries in my code base which means this won't capture many allocations.</li>
  <li># defining malloc to a different symbol. This would force me to push this define into the build settings of all external libraries used and I really want to avoid this.</li>
  </ul>
  <p>Things I don't care about:</p>
  <ul>
  <li>If any of the external libraries are allocating memory in some other way (HeapAlloc, memory mapped files or whatever they come up with), I accept that this won't be tracked properly by overriding malloc.</li>
  </ul>
  <p>The most reasonable solution I can come up with is somehow interfering with the link process and making sure my own malloc is being linked instead of the standard ones, preferably I'd like to be able to use the old malloc functions as default.</p>
  <p>In google <a href="http://code.google.com/p/google-perftools/" rel="nofollow noreferrer">perf-tools</a> it seems like they patch the code of the functions manually at runtime to allow a hook function to be called before calling the original function. Is this really the best way of doing this?</p>