Live data from Hacker News

Falsehoods programmers believe about undefined behavior

predr.ag

231–233 of 233 posts

Re: Falsehoods programmers believe about undefined behavior

#231

Earlier quoted context omitted.

On C UB is a property of any code, reachable or not, just like the article's references explain. Why are people fighting this? And yes, Rust is much more reasonable about it.

How do you decide if i+1 is UB or not, unless the abstract machine reaches that line with a given value of i?

If you write i+1 anywhere on your code, the compiler is free to assume i is different from the maxumim value and rewrite your code with that in mind.

If you are not satisfied, you can read the article's references (there are 2 for unreacheable code). Or any article about UB here for the last 20 years.

Re: Falsehoods programmers believe about undefined behavior

#232

Earlier quoted context omitted.

> It only allows the compiler to assume loops with non-constant expressions can terminate. (And a bunch of other exceptions where it cannot.) That is basically what I said: loops can be assumed to terminate, except for an enumerated set of exceptions where infinite loops are allowed. You seem to be defining "true infinite loop" to mean "while(1)" or "for(;;)" exclusively. But "for (int i = 0; i > What this rule allow…

Infinite loop with a constant expression is defined behavior. for (;;) { }; cannot be assumed to have finite number of executions. Therefore, it cannot be optimized away to finite execution. You're showing potentially infinite loops with non-constant expression as condition. for (; i != 0; ) {} can be assumed to have finite number of executions and can be optimized unless i is constant expression.

> Infinite loop with a constant expression is defined behavior.

Yes, because a constant controlling expression is one of the enumerated exceptions to the rule against infinite loops.

> You're showing potentially infinite loops with non-constant expression as condition.

"for (int i = 0; i I think we are saying the same thing, the only difference is that you seem to be defining "infinite loop" to only include loops with constant controlling expressions, whereas I am defining it to mean "any loop that does not terminate."

Post reply on HN