title: A C++ implementation that detects undefined behavior?
body: <p>A huge number of operations in C++ result in undefined behavior, where the spec is completely mute about what the program's behavior ought to be and allows for anything to happen.  Because of this, there are all sorts of cases where people have code that compiles in debug but not release mode, or that works until a seemingly unrelated change is made, or that works on one machine but not another, etc.</p>
  
  <p>My question is whether there is a utility that looks at the execution of C++ code and flags all instances where the program invokes undefined behavior.  While it's nice that we have tools like valgrind and checked STL implementations, these aren't as strong as what I'm thinking about - valgrind can have false negatives if you trash memory that you still have allocated, for example, and checked STL implementations won't catch deleting through a base class pointer.</p>
  
  <p>Does this tool exist?  Or would it even be useful to have it lying around at all?</p>
  
  <p><strong>EDIT</strong>: I am aware that in general it is undecidable to statically check whether a C++ program may ever execute something that has undefined behavior.  However, it is possible to determine whether a <em>specific execution</em> of a C++ produced undefined behavior.  One way to do this would be to make a C++ interpreter that steps through the code according to the definitions set out in the spec, at each point determining whether or not the code has undefined behavior.  This won't detect undefined behavior that doesn't occur on a particular program execution, but it will find any undefined behavior that actually manifests itself in the program.  This is related to how it is Turing-recognizable to determine if a TM accepts some input, even if it's still undecidable in general.</p>
  
  <p>Thanks!</p>