On 02/18/2013 02:15 PM, Evgeney Knyazhev wrote:
...
Does dv ever get set to 0? If so, then the following line evaluates
a[child+1] when child+1 >= end. If that happens your program has
undefined behavior, and nothing that happens afterwards matters.
dv = (a[child] < a[child+1]) & dv;
James, out there is entire code, you can test it as much as you want. give me array to fail this scheme -- that would be useful ;D
I'm curious. How do you think I could determine that the behavior is
defined by testing it? "Undefined behavior" means that the standard
imposes no requirements on the behavior of your program. The only way to
prove that code has defined behavior is to analyze the code and compare
it to the requirements imposed by the standard. There's no behavior that
could be produced by such a test program that would be inconsistent with
the requirements imposed by the standard on code with undefined
behavior, because there are NO such requirements. When I perform the
appropriate analysis on your code, I find that it violates requirements
imposed by the standard, for which the behavior is undefined.
If you mistakenly believe that a particular piece of code with undefined
behavior is required to produce a particular output, it's entirely
possible that it will. That's actually bad luck for you, because it
prevents you from noticing the defect in your code. This is actually a
disproportionately likely event, because your mistaken certainty about
the behavior will often reflect undue familiarity with the way one
particular system works, and ignorance of the fact that other systems
can, and often do, work differently. You can even get different behavior
just by running the same exact program with the same inputs a second
time. It might even have failed THIS time - it might have done precisely
what you thought it was required to do, and ALSO something else you
didn't think it could do, and you just haven't noticed the other part of
it's behavior yet. Those are the possibilities that makes sane
programmers avoid writing code with undefined behavior.
You should write such code only when something other than the standard
guarantees what the actual behavior will be, and only if you never
intend to port the code to platforms where that guarantee applies. Even
if all of those conditions are met, you still shouldn't write such code
unless you have to.
Can you identify any document that defines what the behavior of your
code should be? If so, what kind of platforms does that document apply
to? Are you sure you'll never want to port this code to any platforms
not on that list?