Re: Possible GCC optimization bug
Arsen Arsenović via Gcc-help <[email protected]> Thu, 12 Feb 2026 18:59:14 +0100
| Newsgroups | gmane.comp.gcc.help |
|---|---|
| Message-ID | <[email protected]> |
Anubis 1101 <[email protected]> writes: > The "10 < 10" comes from the subscript operator overload, where it > checks the index against the Capacity, which is known at compile > time. The "if(i < Capacity)" statement passes, and then prints out > both i and Capacity, which are both 10. Yes, and 'i' is "known" at compile-time to be < 10 due to the mechanism I described earlier. This is due to inlining. Take the following simplified example: int i = /* doesn't matter */; int a[10] = /* doesn't matter */; print(a[i]); /* here, the compiler knows i < 10 due to a[i]. */ This is essentially what happens in your example also. The compiler did a lot of inlining to turn multiple functions into one single function that has pretty much the same pattern as the above in it. It is permitted to do that. It can, in fact, copy length and operator[] into other functions that use them as many times as it likes, as long as it still behaves the same as the abstract machine of the language requires. In fact, 'if (i < Capacity)' probably never happens. The compiler deduces that it is true due to the array accesses. -- Arsen Arsenović
signature.asc
(application/pgp-signature, 418 B)
-----BEGIN PGP SIGNATURE----- iQECBAEWCgCqFiEE/uKz0RP8AKMWLWBhUsKUMB6ixJMFAmmOFPIbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMiwyXxSAAAAAAC4AKGlzc3Vlci1mcHJAbm90YXRpb25z Lm9wZW5wZ3AuZmlmdGhob3JzZW1hbi5uZXRGRUUyQjNEMTEzRkMwMEEzMTYyRDYw NjE1MkMyOTQzMDFFQTJDNDkzEBxhcnNlbkBhYXJzZW4ubWUACgkQUsKUMB6ixJNr FgEAiAMUcsO8PikElTP3aZDyC+/NKHV2kkqCN22rETSZ3f8A/AgpkDCJ3vwh5d00 z4HvBSJPwxacT22Z+jXyaKVpeAQN =9XEw -----END PGP SIGNATURE-----