| Liste des Groupes | Revenir à cl c |
David Brown <david.brown@hesbynett.no> writes:Sure. But if it were not UB, then a conforming implementation could not make such choices or give me such choices. UB does not mean that I definitely have such choices (as my poor wording implies), but that implementations are able to give me the choice.
[...]UB means precisely that I can choose trapping, or IB, or optimising onNo, it means that the implementation can make that choice (or allow you
the assumption it does not happen.
to make that choice). A conforming compiler could generate code on the
assumption that signed overflow never happens, and not give the
programmer any options.
[...]It is obvious - to me, anyway - that signed overflow is a mistake in the code. It is trying to do something that cannot be done. What is the single-digit sum of 5 and 8? There is no answer. The answer is not 3, or 9. Putting your hand in the air and asking the teacher for help might be appropriate sometimes, but it is not a correct answer.
Making this UB is an admission of the blindingly obvious - there is noTrapping or raising/throwing an exception on overflow would also be an
correct answer when signed integer overflow occurs. It tells
programmers that it is a mistake to let your arithmetic overflow, and
it allows tools to help programmers avoid these mistakes, and it
allows compilers to give programmers the most efficient results from
known good code rather than adding unnecessary run-time checks that
are never triggered.
admission of the blindingly obvious.
And a sufficiently clever compilerSure - but in practice having strict overflow checks would significantly reduce optimisation and re-arrangement possibilities, as well as having to include the checks themselves. You might allow non-strict checks in some manner (thus allowing optimisations like "a + b - a" reducing to just "b"), but I think that might be hard to specify and would reduce the debugging help of the checks.
can omit some (not all) checks in cases where it can be statically
proved that overflow doesn't occur, and/or hoist some checks out of
loops.
Of course those kinds of checks are not in the "spirit of C".Indeed.
[...]There are disadvantages in having a trap. It can (depending on hardware) mean extra code to detect the zero - usually that run-time cost is negligible, but sometimes it is not. It will mean extra code to handle the exception - again, often but not always negligible. Those costs apply even if the programmer has made sure that division by zero never occurs. And if a trap is thrown, what then? I think that a programmer that is careful enough to see that a division expression might throw, and handle the trap or exception appropriately, is going to be careful enough to avoid the problem in the first place. So the trap is going to be unexpected and handled badly. A badly handled division by zero exception left the USS Yorktown dead in the water for three hours.
What's the difference between these programs?>I am happy knowing that I cannot divide by 0,Yup. That should be a trap.
For some programs, yes. For others, no.
[...]Checking for syntax errors is cheap - PC computing power is, in this context, pretty much free and unlimited. If I am using a target environment where run-time resources are plentiful, I would not be using C in the first place.
I don't want to pay the price for checks, traps, and limitedI don't want to pay the price of checking for syntax errors when I know
re-arrangements and optimisations when I know my expressions don't
overflow. But I am also happy to be able to get a trap when I ask for
it.
my code is syntactically correct. But I never know that, because I'm
fallible.
I admit that's not a very strong argument. There are real differencesPerhaps I work in a field where that difference is more extreme than for many programmers, and I thus feel it more than most.
between compile-time and run-time checks.
Les messages affichés proviennent d'usenet.