News for hackers. Stuff that matters.
Re:Declaring C String Constants by derefr on Wednesday May 17, 2017 @08:18PM (#14363959)
AFAICT, "undefined behavior" refers to two entirely-separate classes of behavior:

1. cases where the compiler explicitly recognizes a certain permutation of compilation-state as matching some pattern for what the standard calls "undefined behavior"—and then is unconstrained as to what it does in response, but indeed does something in response to detecting that state;

2. cases where the compiler has no compile-time static-analysis approach available for detecting a certain edge-case (e.g. integer overflow), and, further, is inhibited by the standard from generating code to detect that edge-case at runtime either; so the compiler is forced to leave the response to that condition either up to the programmer to explicitly handle, or up to the target architecture to respond to however it likes when it happens.

It's clear to me that undefined behavior of the second class is sort of just part of what C "is": a language where the complexity-classes of all the primitives [well, except FP math] are constrained at compile-time, so a line of code that's O(1) on one arch won't get runtime-polyfilled into being O(N) on another in order to "make the arch sane."

But my question, then, is this: would it break anything about support for the "not C machine" architectures, if the standard said that everything falling into the first class—explicit undefined behavior—was instead defined behavior, defined as "abort compilation"?

Re:Declaring C String Constants by iainmerrick on Wednesday May 17, 2017 @08:26PM (#14363996)
I don't know, but I would dearly love to use a compiler that does that.
Re:Declaring C String Constants by gumby on Thursday May 18, 2017 @12:28AM (#14364817)
> I don't know, but I would dearly love to use a compiler that does that.

Well you're in luck, since gcc -Werror when used with many of its -W flags will do precisely that.

You guys should read the minutes of committee discussions and position papers. "undefined behavior" is not something introduced to make programmers' lives hard!

Also look at IEEE734 which does define things to an incredible degree -- it's extremely hard to use (as it needed to be!). It's one of the very best standards of all time, and still has, yes, undefined behavior.

Re:Declaring C String Constants by derefr on Thursday May 18, 2017 @12:35AM (#14364835)
> Well you're in luck, since gcc -Werror when used with many of its -W flags will do precisely that.

That's not lucky; I'd argue it's the opposite. An ecosystem built around compilers that default to not using -Werror (or rather: `-Werror=pedantic -pedantic-errors`) is an ecosystem that encourages people to write code that relies on the optimizations compilers make in response to undefined behavior. Which, undoubtedly, "makes [maintenance] programmers' lives hard," when they change one little thing and suddenly the code gets 10x slower because now e.g. a dead loop isn't being elided.

If all compilers defaulted to something like `-Werror=pedantic -pedantic-errors`—and it was highly discouraged to turn those flags off—then jumping in to contribute to a portable, sprawling codebase would be infinitely simpler, because the codebase would have been written in a far more rigorous way up to that point.

Sunlight is the best disinfectant—so why do we allow our compiler-vendors to default the lights to off?

Re:Declaring C String Constants by iainmerrick on Thursday May 18, 2017 @11:28AM (#14368032)
Amen! I'm not a big Go fan but Go definitely has the better approach here.
Re:Declaring C String Constants by iainmerrick on Thursday May 18, 2017 @11:24AM (#14367989)
To clarify, it's not actually undefined behavior per se that I'm complaining about, it's the modern trend of highly optimizing compilers which take undefined behavior as an excuse to do all sorts of ridiculous things to your code.

"You may have thought you were checking for overflow here, but technically speaking the result of that operation could be undefined, therefore this code could do literally anything, or nothing. So I'll just remove it all. Congratulations, now it runs faster!"

I honestly hadn't realized that was a controversial view. Doesn't almost everyone dislike that? Are there a lot of C programmers who are happy to accept that risk in exchange for slightly better optimization?