44 points · by luu · 11y ago · 35 comments
Ignoring the original portability concerns, there's a large of set of optimizations that are only possible if you can assume that no undefined behaviour occurs.
Accessing past the bounds of an array is undefined, and generally a bad thing to do. If the compiler decides that a block of code could only run if you access outside of the array, why not delete that code? Surely, it'll never run!
Even eliminating array bounds checking is an optimization that requires the assumption that you don't go past the end of the array. Languages like Java and Python pay a premium to ensure you don't do this on each iteration of your for loops.
So it being UB means you've got a compiler which can optimize better but the cost is that you tied the programmer's hands, so the programmer's optimization opportunities are in fact reduced. And you didn't even tie them, the annoying thing is, rather you laid traps that they can fall into because of failing to notice them.
(I'm not saying C made the wrong tradeoff with declaring this or that UB, just that the tradeoff exists.)
Oh, the hilarious irony of giving language-lawyery, glib responses of "obviously, the code will not run" to users (who probably, you know, wrote code with the intention of it running) - users who are complaining about language lawyering optimizing compilers in the first place. It's like two people reading the same page in a different book or something.
int ComputeStuff(int value) {
if(value < 27) {
long and complex computation specialized for values under 27
return result
} else {
long and complex computation specialized for values 27 or more
return result
}
}
Then I call it from somewhere else like so: int x = ComputeStuff(12);
Let's say the compiler decides this is a good candidate for inlining. Since the programmer wrote code with the intention of it running, are you saying that the compiler should not take advantage of the fact that it knows the exact value being passed into the function in this case and can delete half the code knowing it will never run?And, for Java, JITs do a lot of work to remove bounds checks from loops over arrays so that you end up with the fastest possible machine code.
As you say, these days smart compilers can optimise to reduce the overhead of the bounds check; but the original 70s C compilers didn't try to be that smart.
I don't want to duplicate the code for every single context it is used in -- so I'm happy the compiler can throw away pieces of the code that aren't relevant in each inlined context.
OTOH, for ordinary non-inlined code, I really want a warning if my code is thrown away or optimized in a surprising manner.
Indeed, gcc and clang try to behave according to the two ideas above. gcc violates this terribly with its removal of dead code warnings -- which may be eliminated but no warnings are generated.
>variable 'c' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
If you stick to what is taught in any good tutorial/book, you don't even have to think about problems like this.
But if you decide to play with fire, then you should read the Standard and understand it.
C is not a scripting language. If you use the tools available to you, and don't abuse the language, then it is fairly hard to cause undefined behavior.
float c;
if(get(v, &c))
...do something with c...
instead of the more verbose bool ok;
float c;
c = get(v, &ok);
if(ok)
...do something with c...The first one enables you to have the function call directly in the if statement, but requires you to define a variable beforehand.
The latter gives you the option to check the return value, pass a NULL, if you don't need it for example, and use the return value directly.
Further, does the signalling NaN behavior happen with SSE (or NEON) or is this an x87 issue?
IIRC, FSTP st(0), to simply clear the stack without using the result as discussed in the article, doesn't even generate #IA, so it can't trap or raise invalid (it only generates #IA when the store converts to a smaller FP type (fun fact: this is so FLD/FSTP could be used to implement memcpy way back when))
6.3.2.1,p2 If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.