Re: Possible GCC optimization bug

Anubis 1101 via Gcc-help <[email protected]> Thu, 12 Feb 2026 18:25:33 +0000
Newsgroups gmane.comp.gcc.help
Message-ID <3icsV6qBJk4lPvbZLlBOpK-aWKu8ca2MjO9XTHpgT78iHxrHgXzkM7SpxmVkS3gAdixtp3aUn3hl9uYxtkQxVSGar4lH_P5YKw_bWezSK88=@pm.me>
> 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.

I did not know about that, that's interesting stuff!
But the const char* version works, so I'll look into that. I figured it was
inlining the wrong value, but even if I replaced both size() and length() with
strlen(), the measured value was the same, so I figured that couldn't be it.
Perhaps the const char* version works because it's using manual memory
calculation?

I'll have to do some messing around and see what I can find out.


On Thursday, February 12th, 2026 at 10:59 AM, Arsen Arsenović <[email protected]> wrote:

> 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ć
>