Re: Constants and undefined behavior

Liste des GroupesRevenir à cl c  
Sujet : Re: Constants and undefined behavior
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 15. Jun 2026, 09:09:56
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <110oc0k$6rfi$1@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 15/06/2026 00:55, Keith Thompson wrote:
David Brown <david.brown@hesbynett.no> writes:
[...]
UB means precisely that I can choose trapping, or IB, or optimising on
the assumption it does not happen.
 No, it means that the implementation can make that choice (or allow you
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.
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.
If the standards had said integer overflow was IB, then that puts limits on what the compiler can do - and therefore on what it can do to help the programmer.  Exactly what options it had would depend on the wording of the standard, such as whether it required an "implementation-defined value" or, like narrowing conversions to signed integer types, "either the result is implementation-defined or an implementation-defined signal is raised".  However, even in that later case I think it would be more confusing for a lot of programmers - many programmers, quite reasonably, have an intuition that "UB" means "don't do this" or "this is not legal in C".  They also have the intuition that "IB" means "this works according to the underlying hardware".  If the standards had said integer overflow was IB, most programmers would immediately assume that meant wrapping behaviour.
More interesting, I think, is the possible future "erroneous behaviour" marker.  My understanding is that it lets the compiler have traps or other run-time detection, or provide unspecified values, while making it clear that erroneous behaviour is a result of software bugs.

 [...]
 
Making this UB is an admission of the blindingly obvious - there is no
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.
 Trapping or raising/throwing an exception on overflow would also be an
admission of the blindingly obvious. 
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.
Throwing some kind of exception or trap can definitely be helpful at times.  And I agree that it would make it obvious that there has been a problem detected.  But throwing exceptions or traps can cause more problems (the Ariane 5 failure was caused by the exception handler, not the overflow fault).  That does not mean it is better to ignore overflows - it means there is no appropriate action that is suitable in every situation.  I am far from convinced that there is even a reasonable choice of default action that could be usefully made.

And a sufficiently clever compiler
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.
Sure - 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.

 Of course those kinds of checks are not in the "spirit of C".
 
Indeed.
And if we want to move away from the "spirit of C", then I think we should move away from the /language/ of C.  In C, people do not expect exceptions or sudden jumps from their code - they expect that if there is checking for errors, it is explicit in the code.  In many other languages, there is a much clearer understanding that lots of things can fail and cause immediate exits from the function - and code is (hopefully!) written to handle that.

[...]
 
I am happy knowing that I cannot divide by 0,
Yup.  That should be a trap.
>
For some programs, yes.  For others, no.
 What's the difference between these programs?
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.
Is it better /not/ to trap?  There is no general rule.  If you have tried to divide by zero, something has gone wrong before the division, and there are no good answers to what will go wrong afterwards. Sometimes it is possible to do damage limitation - sometimes not.
The correct way to handle the situation is to avoid it - be sure that you are not dividing by zero in the first place.  Identify and handle the problem where it occurs - when this zero is created, or the circumstances leading to that point - rather than trying to do a post-mortem after the failed division.  And if you are doing that, then what benefit is there in having trapping for division by zero?  It becomes just a waste of effort.
(There are other ways of handling such things, like the use of NaN's in floating point, or extending your integers with some kind of "invalid" indicators.)

 [...]
 
I don't want to pay the price for checks, traps, and limited
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.
 I don't want to pay the price of checking for syntax errors when I know
my code is syntactically correct.  But I never know that, because I'm
fallible.
 
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 admit that's not a very strong argument.  There are real differences
between compile-time and run-time checks.
 
Perhaps I work in a field where that difference is more extreme than for many programmers, and I thus feel it more than most.

Date Sujet#  Auteur
27 May 26 * this girl calls c ugly363fir
27 May 26 `* Re: this girl calls c ugly362fir
28 May 26  `* Re: this girl calls c ugly361BGB
28 May 26   +* Re: this girl calls c ugly5Lawrence D’Oliveiro
28 May 26   i+* Re: this girl calls c ugly3BGB
29 May 26   ii`* Re: this girl calls c ugly2Lawrence D’Oliveiro
29 May 26   ii `- Re: this girl calls c ugly1BGB
28 May 26   i`- Re: this girl calls c ugly1Bonita Montero
28 May 26   +* Re: this girl calls c ugly19Janis Papanagnou
28 May 26   i+* Re: this girl calls c ugly15BGB
29 May 26   ii+- Re: this girl calls c ugly1Lawrence D’Oliveiro
29 May 26   ii`* Re: this girl calls c ugly13Janis Papanagnou
29 May 26   ii `* Re: this girl calls c ugly12BGB
29 May 26   ii  +* Re: this girl calls c ugly9David Brown
29 May 26   ii  i`* Re: this girl calls c ugly8BGB
30 May 26   ii  i `* Re: this girl calls c ugly7David Brown
30 May 26   ii  i  +* Re: this girl calls c ugly2Janis Papanagnou
30 May 26   ii  i  i`- Re: this girl calls c ugly1David Brown
30 May 26   ii  i  `* Re: this girl calls c ugly4BGB
31 May 26   ii  i   `* Re: this girl calls c ugly3David Brown
31 May 26   ii  i    `* Re: this girl calls c ugly2BGB
31 May 26   ii  i     `- Re: this girl calls c ugly1David Brown
29 May 26   ii  +- Re: this girl calls c ugly1Janis Papanagnou
30 May 26   ii  `- Re: this girl calls c ugly1Lawrence D’Oliveiro
28 May 26   i`* Re: this girl calls c ugly3Chris M. Thomasson
29 May 26   i `* Re: this girl calls c ugly2Janis Papanagnou
29 May 26   i  `- Re: this girl calls c ugly1Chris M. Thomasson
28 May 26   `* Re: this girl calls c ugly336fir
28 May 26    `* Re: this girl calls c ugly335BGB
29 May 26     +* Re: this girl calls c ugly328Lawrence D’Oliveiro
29 May 26     i`* Re: this girl calls c ugly327Janis Papanagnou
29 May 26     i `* Re: this girl calls c ugly326Bart
29 May 26     i  +* Re: this girl calls c ugly310Janis Papanagnou
29 May 26     i  i`* Re: this girl calls c ugly309Bart
29 May 26     i  i +* Re: this girl calls c ugly9Janis Papanagnou
29 May 26     i  i i+* Re: this girl calls c ugly2Bart
29 May 26     i  i ii`- Re: this girl calls c ugly1Janis Papanagnou
29 May 26     i  i i`* Re: this girl calls c ugly6Bart
29 May 26     i  i i +* Re: this girl calls c ugly4Janis Papanagnou
29 May 26     i  i i i`* Re: this girl calls c ugly3Bart
29 May 26     i  i i i `* Re: this girl calls c ugly2Janis Papanagnou
29 May 26     i  i i i  `- Re: this girl calls c ugly1Bart
29 May 26     i  i i `- Re: this girl calls c ugly1Keith Thompson
29 May 26     i  i `* Re: this girl calls c ugly299tTh
29 May 26     i  i  `* Re: this girl calls c ugly298Bart
29 May 26     i  i   +* Re: this girl calls c ugly296Keith Thompson
29 May 26     i  i   i`* Re: this girl calls c ugly295Bart
29 May 26     i  i   i +- Re: this girl calls c ugly1Janis Papanagnou
29 May 26     i  i   i `* Re: this girl calls c ugly293Keith Thompson
29 May 26     i  i   i  `* Re: this girl calls c ugly292Bart
29 May 26     i  i   i   +* Re: this girl calls c ugly5Keith Thompson
30 May 26     i  i   i   i`* Re: this girl calls c ugly4James Kuyper
30 May 26     i  i   i   i `* Re: this girl calls c ugly3Bart
30 May 26     i  i   i   i  `* Re: this girl calls c ugly2Keith Thompson
30 May 26     i  i   i   i   `- Re: this girl calls c ugly1Bart
30 May 26     i  i   i   `* Re: this girl calls c ugly286Dan Cross
30 May 26     i  i   i    +* Re: this girl calls c ugly282Bart
31 May 26     i  i   i    i+* Re: this girl calls c ugly280Keith Thompson
31 May 26     i  i   i    ii+* Re: this girl calls c ugly5Janis Papanagnou
31 May 26     i  i   i    iii+* Re: this girl calls c ugly2Keith Thompson
2 Jun 26     i  i   i    iiii`- Re: this girl calls c ugly1Janis Papanagnou
31 May 26     i  i   i    iii`* Re: this girl calls c ugly2David Brown
2 Jun 26     i  i   i    iii `- Re: this girl calls c ugly1Janis Papanagnou
31 May 26     i  i   i    ii`* Re: this girl calls c ugly274Richard Harnden
31 May 26     i  i   i    ii +* Re: this girl calls c ugly169David Brown
31 May 26     i  i   i    ii i+* Re: this girl calls c ugly166Bart
31 May 26     i  i   i    ii ii+* Re: this girl calls c ugly140David Brown
31 May 26     i  i   i    ii iii`* Re: this girl calls c ugly139James Kuyper
31 May 26     i  i   i    ii iii `* Re: this girl calls c ugly138David Brown
31 May 26     i  i   i    ii iii  +* Re: this girl calls c ugly4James Kuyper
31 May 26     i  i   i    ii iii  i`* Re: this girl calls c ugly3David Brown
31 May 26     i  i   i    ii iii  i `* Re: this girl calls c ugly2James Kuyper
1 Jun 26     i  i   i    ii iii  i  `- Re: this girl calls c ugly1David Brown
31 May 26     i  i   i    ii iii  `* Re: this girl calls c ugly133Keith Thompson
1 Jun 26     i  i   i    ii iii   +* Re: this girl calls c ugly2David Brown
1 Jun 26     i  i   i    ii iii   i`- Re: this girl calls c ugly1Keith Thompson
2 Jun 26     i  i   i    ii iii   +* Re: this girl calls c ugly129Janis Papanagnou
2 Jun 26     i  i   i    ii iii   i+- Re: this girl calls c ugly1James Kuyper
2 Jun 26     i  i   i    ii iii   i+* Constants and undefined behavior82Tim Rentsch
2 Jun 26     i  i   i    ii iii   ii`* Re: Constants and undefined behavior81Dan Cross
4 Jun 26     i  i   i    ii iii   ii `* Re: Constants and undefined behavior80Tim Rentsch
4 Jun 26     i  i   i    ii iii   ii  `* Re: Constants and undefined behavior79Dan Cross
4 Jun 26     i  i   i    ii iii   ii   +* Re: Constants and undefined behavior31Keith Thompson
5 Jun 26     i  i   i    ii iii   ii   i+* Re: Constants and undefined behavior28Dan Cross
5 Jun 26     i  i   i    ii iii   ii   ii+* Re: Constants and undefined behavior24Keith Thompson
6 Jun 26     i  i   i    ii iii   ii   iii+* Re: Constants and undefined behavior19Dan Cross
6 Jun 26     i  i   i    ii iii   ii   iiii`* Re: Constants and undefined behavior18Keith Thompson
8 Jun 26     i  i   i    ii iii   ii   iiii `* Re: Constants and undefined behavior17Dan Cross
8 Jun 26     i  i   i    ii iii   ii   iiii  +* Re: Constants and undefined behavior5Keith Thompson
9 Jun 26     i  i   i    ii iii   ii   iiii  i`* Re: Constants and undefined behavior4Dan Cross
9 Jun 26     i  i   i    ii iii   ii   iiii  i `* Re: Constants and undefined behavior3Keith Thompson
9 Jun10:46     i  i   i    ii iii   ii   iiii  i  `* Re: Constants and undefined behavior2Dan Cross
9 Jun23:07     i  i   i    ii iii   ii   iiii  i   `- Re: Constants and undefined behavior1Keith Thompson
9 Jun 26     i  i   i    ii iii   ii   iiii  `* Re: Constants and undefined behavior11Waldek Hebisch
9 Jun23:29     i  i   i    ii iii   ii   iiii   +* Re: Constants and undefined behavior3James Kuyper
10 Jun00:01     i  i   i    ii iii   ii   iiii   i`* Re: Constants and undefined behavior2Keith Thompson
10 Jun13:36     i  i   i    ii iii   ii   iiii   i `- Re: Constants and undefined behavior1Dan Cross
11 Jun15:49     i  i   i    ii iii   ii   iiii   `* Re: Constants and undefined behavior7Janis Papanagnou
11 Jun16:20     i  i   i    ii iii   ii   iiii    +* Re: Constants and undefined behavior2Dan Cross
11 Jun17:08     i  i   i    ii iii   ii   iiii    i`- Re: Constants and undefined behavior1Janis Papanagnou
11 Jun17:30     i  i   i    ii iii   ii   iiii    `* Re: Constants and undefined behavior4Waldek Hebisch
6 Jun 26     i  i   i    ii iii   ii   iii`* Re: Constants and undefined behavior4Tim Rentsch
5 Jun 26     i  i   i    ii iii   ii   ii`* Re: Constants and undefined behavior3Janis Papanagnou
7 Jun 26     i  i   i    ii iii   ii   i`* Re: Constants and undefined behavior2Tim Rentsch
9 Jun07:05     i  i   i    ii iii   ii   `* Re: Constants and undefined behavior47Tim Rentsch
2 Jun 26     i  i   i    ii iii   i`* Re: this girl calls c ugly45Keith Thompson
2 Jun 26     i  i   i    ii iii   `- Re: this girl calls c ugly1Chris M. Thomasson
2 Jun 26     i  i   i    ii ii`* Re: this girl calls c ugly25Dan Cross
31 May 26     i  i   i    ii i`* Re: this girl calls c ugly2James Kuyper
31 May 26     i  i   i    ii +* Re: this girl calls c ugly2Keith Thompson
31 May 26     i  i   i    ii `* Re: this girl calls c ugly102Tim Rentsch
31 May 26     i  i   i    i`- Re: this girl calls c ugly1Dan Cross
1 Jun 26     i  i   i    `* Re: this girl calls c ugly3Tim Rentsch
30 May 26     i  i   `- Re: this girl calls c ugly1David Brown
29 May 26     i  +* Re: this girl calls c ugly6Janis Papanagnou
30 May 26     i  `* Re: this girl calls c ugly9Lawrence D’Oliveiro
29 May 26     `* Re: this girl calls c ugly6Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal