Re: Optimisations and undefined behaviour

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2015-11-08 20:33:59 +0100, David Brown wrote:
> On 08/11/15 20:11, Florian Weimer wrote:
> >On 11/06/2015 01:32 PM, David Brown wrote:
> >>How about this case:
> >>
> >>int foo(int x) {
> >>	if (x > 1290) {
> >>		printf("X is wrong here %d, but we don't care\n", x);
> >>	}
> >>	return x*x*x;
> >>}
> >>
> >>The compiler can eliminate the check and the printf.
> >
> >I don't think the compiler can do that because printf has an externally
> >visible effect, which is sequenced before the undefined behavior, so
> >this program transformation would not be permitted under the as-if rule.
> >
> 
> My understanding (and correct me if it's wrong) is that undefined behaviour
> is a feature of a program, rather than just part of it - i.e., if code
> reaches something that causes undefined behaviour, the effect of the whole
> program is undefined.  This means that if the compiler knows that it is
> going to hit an integer overflow, it can affect the course of things that
> would have happened without that undefined behaviour.

I agree. However, one may wonder whether the return statement is
always reachable. I mean that since printf does I/O, it might
block forever. But I'm not sure...

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux