4 ms·
Since this is likely to degenerate into a discussion on the merits and pitfalls of optimizing undefined behavior, how about a look at this thread on mechanicals
by eloff 11y ago
Since this is likely to degenerate into a discussion on the merits and pitfalls of optimizing undefined behavior, how about a look at this thread on mechanicalsympathy where the topic was beat to death just a short while ago:
https://groups.google.com/forum/#!topic/mechanical-sympathy/jAqF8f0HdM0 https://groups.google.com/forum/#!topic/mechanical-sympathy/...
One of the juicier replies discussing some of the undefined behavior in C (by Rajiv Kurian):
1. A program in a hosted environment does not define a function named main using one of the specified forms - not a compile time error, it's UB.
2. The arguments to certain operators are such that could produce a negative zero result, but the implementation does not support negative zeros.
3. Two declarations of the same object or function specify types that are not compatible - again not a compilation error, it's UB.
4. An unmatched ' or " character is encountered on a logical source line during tokenization. During tokenization!!
5. An exceptional condition occurs during the evaluation of an expression - how specific.
6. An exceptional condition occurs during the evaluation of an expression - hmmm
7. A structure or union is defined as containing no named members - ookay.
8. The value of an unnamed member of a structure or union is used - why is there a compiler at all!
9. The character sequence in an #include preprocessing directive does not start with a letter - lol
10. The program modifies the string pointed to by the value returned by the setlocale function - dear god.
11. The string set up by the getenv or strerror function is modified by the program .
12. The array being searched by the bsearch function does not have its elements in proper order - not a logical error just straight up UB.
This is the tip of the iceberg. How confident is any one now that their program doesn't invoke UB? Things many programmer would assume are compiler errors are actually UB. What would happen if gcc/clang exploited each one of these?
- MichaelBurge 11y agoI always write my C code with a C++ compiler, since it has some quality-of-life improvements. I think C++ is free to define undefined behavior without worrying about backwards compatibility, so I wonder how many of those still apply if you use a C++ compiler.
- caf 11y ago10 and 11 are effectively just saying that the string returned by setlocale(), getenv() or strerror() might be a string constant, which are already unmodifiable.
- rdc12 11y ago12. Well breaking the invariants of a function is never a good idea, but putting defined behaviour in that case would put substational restrictions on how the function could be implemented (and still almost certainly do something weird anyway latter)
- umanwizard 11y agoIt could be implementation-defined without being UB.
- simcop2387 11y agoDoes anybody have an example of 7 and 8? I can't even think of how you'd accomplish them right now.
- pcwalton 11y ago> This is the tip of the iceberg. How confident is any one now that their program doesn't invoke UB? Things many programmer would assume are compiler errors are actually UB. What would happen if gcc/clang exploited each one of these? This is a pointless question. There is no reason for gcc and clang to exploit these, as there are no performance wins they could get by doing so. The analogy you're giving presupposes that the developers of those compilers like making code insecure for fun, because they're language lawyers. They aren't doing that. They're doing these optimizations because of documented, measurable performance gains, usually because someone filed a bug against the compiler asking why it didn't optimize their code in the way they thought it should.