title: Check if a pointer points to allocated memory on the heap
body: <p>I want to know if a pointer points to a piece of memory allocated with malloc/new.  I realize that the answer for an arbitrary address is "No you can't" but I do think it is possible to override malloc/free and keep track of allocated memory ranges.</p>
  
  <p>Do you know a memory management library providing this specific tool?<br>
  Do you know something for production code?</p>
  
  <p><a href="http://valgrind.org/" rel="noreferrer">Valgrind</a> is great, but it is too much instrumentation (slow) and as Will said we don't want to use Valgrind like this (making the soft crash is good enough).<br>
  <a href="http://gcc.gnu.org/wiki/Mudflap_Pointer_Debugging" rel="noreferrer">Mudflap</a> is a very good solution, but dedicated to GCC, and sadly, a check does not simply return a boolean (see my answer below).<br>
  Note that checking that memory writes are legal is a <a href="http://en.wikipedia.org/wiki/Buffer_overflow" rel="noreferrer">security issue</a>. So looking for performance is motivated.</p>