News for hackers. Stuff that matters.
Re:Re: C as used/implemented in practice by mrpippy on Monday July 13, 2015 @07:59PM (#9881975)
For the for loop example, is there some reason why clang doesn't output a warning like "Does 'i' really need to be signed? If so, explicitly make it a 'signed int'. Otherwise, change it to be unsigned"
Re:Re: C as used/implemented in practice by porges on Monday July 13, 2015 @08:30PM (#9882118)
The point here is not particularly about signedness, it's that UB allows better optimizations to be performed.

If overflow is defined to wrap around then it's potentially an infinite loop (take N == MAXVALUE). With overflow defined as UB you can say the loop executes exactly N times (because you're not allowed to write code that overflows).

So UB is both bad and a source of power :)

Re:Re: C as used/implemented in practice by cowsandmilk on Monday July 13, 2015 @08:44PM (#9882166)
> The point here is not particularly about signedness

But in the case of C, that is what it is about since unsigned integers have defined behavior, so you can only have UB and the optimizations when you use a signed integer.

Re:Re: C as used/implemented in practice by jeffreyrogers on Monday July 13, 2015 @08:47PM (#9882180)
Yes, but more generally UB allows optimizations that wouldn't be allowed otherwise. The whole reason C has so many undefined behaviors is for the benefit of compiler writers.
Re:Re: C as used/implemented in practice by Joky on Monday July 13, 2015 @09:11PM (#9882260)
I believe it is not the original reason: most UB are about funky HW. For instance for the non-wrap signed int it can be explained because C does not assume you have 2-complement hardware.
Re:Re: C as used/implemented in practice by jcranmer on Tuesday July 14, 2015 @12:37AM (#9882916)
More specifically, some early hardware would trap on signed overflow. A lot of undefined behavior in C actually comes from "some machine would cause a trap", and C predates the invention of precise trapping in out-of-order processors. The possibility of traps is generally the difference between undefined behavior or unspecified/implementation-defined behavior.
Re:Re: C as used/implemented in practice by GFK_of_xmaspast on Monday July 13, 2015 @09:57PM (#9882398)
If N is unsigned, clang++ (and I think g++) will warn about signed/unsigned comparison, but sometimes you do want signed loop indices.
Re:Re: C as used/implemented in practice by tacos on Tuesday July 14, 2015 @02:06AM (#9883138)
Microsoft's compiler has been ALL OVER THIS for 20 years.

Nothing says "buggy port of Linux app to Windows" faster than "#pragma warning (disable: 4018)".

Clang is catching up. GCC doesn't seem to care as much. Given all the bitching about undefined behavior you'd think they'd up the warnings.

Compilers are programs too, y'know. We can define (and gasp, re-define) behavior.

Re:Re: C as used/implemented in practice by GFK_of_xmaspast on Tuesday July 14, 2015 @09:58PM (#9889291)
Well, in fairness and generality nothing says buggy like "disabled warnings".

(and you know you're in trouble when you start looking at a new codebase and see a "-fpermissive" in there)

Totally agree with more warnings being needed, I understand that exploiting UB is needed for optimization, but I don't buy the 'well we can't warn about every case, so we won't warn about any' reasoning.