3 ms·
It allows the optimizer to assume away the halting problem; all nontrivial loops are obligated to halt.
by bcoates 10y ago
It allows the optimizer to assume away the halting problem; all nontrivial loops are obligated to halt.
- Gibbon1 10y agoI read this as, will confuse the snot out of hapless newbie programmers trying to learn C via stepping though their code in an IDE. While providing no practical benefit to programmers writing production code.
- blackflame7000 10y agoIt is necessary in order for the compiler to do transformation optimizations which do impact production code. Newbies shouldn't be writing production code without guidance anyways IMO.
- Thrillington 10y agoNewbies should probably turn off optimization entirely.
- blackflame7000 10y agoNo argument there, or at the very least whole program optimizations
- adamnemecek 10y agoWhat's the point of this though? Why are you letting programmers write non-functional code? When does the loop exactly terminate? I'm guessing the standard discusses this but at this point idk if I care about memorizing more C trivia.
- msbarnett 10y ago> What's the point of this though? Why are you letting programmers write non-functional code? He just told you. Because the only way to prevent it in general is to solve the halting problem. > When does the loop exactly terminate? In the general case this is provably impossible to determine. (all you're seeing here is that the compiler authors felt no need to add special case logic to handle "trivial" cases of the halting problem. If the compiler sees any expression in a loop test, it assumes the loop will halt some of the time)
- adamnemecek 10y ago> He just told you. Because the only way to prevent it in general is to solve the halting problem. I'm aware. I think that I might have misunderstood what the optimization really does.
- msbarnett 10y agoGrandparent presented this as a "surprising optimization" but I'd argue it's exactly what you'd expect when a compiler sees while(expression) -- he just happened to pick a trivial expression.
- deleted 10y ago[deleted]
- mnarayan01 10y agoThe idea (I assume) is to let compilers optimize away loops without determining that they terminate. I.e. the rule is more aimed at loops which would terminate, but this lets compilers avoid proving that they do in fact terminate.
- deleted 10y ago[deleted]
- CJefferson 10y agoIt's more useful in C++. Imagine a templated vector destructor: for(T* ptr = begin; ptr != end; ++ptr) { destroy(ptr); } For many types, 'destroy' will be empty (for example integers), so this just turns into: for(T* ptr = begin; ptr != end; ++ptr) { } Which is an empty loop and can be optimised away -- assuming we can prove it isn't an infinite loop! Which can be quite hard sometimes (in this case, it needs that 'end' is some multiple of sizeof(T) bigger than begin). Now in many cases you can prove these loops are finite, but in full generality it's quite hard, and it was considered easier to just let the compiler remove them.
- dbaupp 10y agohttp://www.open-std.org/jtc1/sc22/wg14/www/docs/n1528.htm http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1528.htm discusses it in some detail.