4 ms·
On a typical modern arch, there are consistent and reasonable answers: 1. 8 (https://godbolt.org/z/Udcuj0 https://godbolt.org/z/Udcuj0) 2. 0 (https://godbolt.
by ioquatix 7y ago
On a typical modern arch, there are consistent and reasonable answers:
1. 8 (https://godbolt.org/z/Udcuj0 https://godbolt.org/z/Udcuj0)
2. 0 (https://godbolt.org/z/kZz1YQ https://godbolt.org/z/kZz1YQ)
3. -96 (https://godbolt.org/z/1Xq_Oo https://godbolt.org/z/1Xq_Oo)
4. 1 (https://godbolt.org/z/MIo0s_ https://godbolt.org/z/MIo0s_)
5. 2 (https://godbolt.org/z/J0dsVW https://godbolt.org/z/J0dsVW)
I agree, you can argue that it's unspecified, undefined, or whatever. It might not be well defined by the C specification, but none of these programs produce surprising output. Programming in the real world requires that you are able to read and write code like this, even if it requires that you investigate (and depend) the specific behaviour of your compiler/platform.
- 3JPLW 7y ago> produce well defined output. Perhaps “produce consistent results” would be more appropriate.
- seba_dos1 7y agoSwitch the compiler to ARM, which is as "typical modern arch" as it gets, and see for yourself.
- ioquatix 7y agoI don't think it's surprising when you switch machine architecture and/or word sizes that you get different results. In fact for me, that's completely normal and to be expected.
- ryandrake 7y agoSorry, but it’s this kind of attitude that leads to impossible-to-port code, platform lock-in, subtle bugs when tool chains change, and worst case: buffer overflows and outages. These programs may today produce “well defined” output on your favorite systems, but that doesn’t change the fact that it is invoking various flavors of undefined, implementation-defined behavior. It’s not safe C just because it works for me. It’s an unfortunate truth that programming in the real world involves programmers who dare to explore these corners of C and claim to have answers to these questions. Stay away! Knowing C means knowing what is not defined as much as knowing what is.
- _kst_ 7y agoNo, for the cases that involve undefined behavior, the results can differ arbitrarily depending on compiler settings, optimization settings, the presence of seemingly irrelevant code, or, in principle, the phase of the moon. The behavior of any program that evaluates `i++ + ++i` is undefined. The solution is not to find out how it happens to behave in some circumstances. It's to find clearer code that expresses whatever the original intent was.
- erik_seaberg 7y agoYou have to know what the standard allows because every optimizer change tries to be more aggressive without violating it. People have been bitten by error checks whose object code was elided because the error "can't happen". You have to decide whether you need code that will always work, or code that seemed to work for a while.
- TazeTSchnitzel 7y agoConsistent as in works for me and I ran it twice!, not consistent as in guaranteed to continue working after sudo apt upgrade gcc. Undefined behaviour can and will be used to make assumptions for optimisations that will bite you.