On Tue, 2013-05-14 at 04:50 -0400, Daniel Micay wrote:
On Tue, May 14, 2013 at 3:49 AM, <goodmenzy@gmail.com> wrote:
If this function initArr() is moved to the same cpp file of main(), all optimization level get the same result.
Good observation. Having a separate TU for init function was intentional to trigger the problem.
That's the great thing about undefined behaviour, you never know what you'll get. It's really not something anyone should be relying on though.... there's a well defined, valid way of declaring a flexible-length struct member.
There is no magic or random involved. The compiler interpret your code and take decisions on what it thinks is good for you. If it sees the init function, it figure outs that it cannot optimize away the loop. If you hides the initialization by calling lets say a shared library function, the compiler will make assumption that it is safe to optimize the loop.