Optimisations and undefined behaviour

Florian Weimer fweimer@redhat.com
Sun Nov 8 19:11:00 GMT 2015

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.


More information about the Gcc-help mailing list