title: How can I detect if a program is running from within valgrind?
body: <p>Is there a way to identify at run-time of an executable is being run from within valgrind?  I have a set of C++ unit tests, and one of them expects <code>std::vector::reserve</code> to throw <code>std::bad_alloc</code>.  When I run this under valgrind, it bails out completely, preventing me from testing for both memory leaks (using valgrind) and behavior (expecting the exception to be thrown).</p>
  
  <p>Here's a minimal example that reproduces it:</p>
  
  <pre><code>#include &lt;vector&gt;
  int main()
  {
      size_t uint_max = static_cast&lt;size_t&gt;(-1);
      std::vector&lt;char&gt; v;
      v.reserve(uint_max);
  }
  </code></pre>
  
  <p>Running valgrind, I get this output:</p>
  
  <pre><code>Warning: silly arg (-1) to __builtin_new()
  new/new[] failed and should throw an exception, but Valgrind
     cannot throw exceptions and so is aborting instead.  Sorry.
     at 0x40192BC: VALGRIND_PRINTF_BACKTRACE (valgrind.h:319)
     by 0x401C823: operator new(unsigned) (vg_replace_malloc.c:164)
     by 0x80487BF: std::vector&lt;char, std::allocator&lt;char&gt; &gt;::reserve(unsigned) new_allocator.h:92)
     by 0x804874D: main (vg.cxx:6)
  </code></pre>
  
  <p>I'd like to modify my unit test to simply skip the offending code when it's being run from within valgrind.  Is this possible?</p>