Given that this is complete code, the compiler is not required to do anything, because the code is nonsense. The compiler is free to optimize the entire cycle because it does nothing.
A fully standard compatible compiler must initialize both objs and srcData for all zeros, since they have a static storage duration.
Therefore, a nested loop does nothing but skew zeros from one array to another. If the compiler notices that this loop is pointless, it can completely remove the loop.
Of course, it makes no sense to reduce the number of iterations in the loop as a way of optimization, so you may wonder what strange decisions the optimizer made in order to come up with this machine code. Odd and dumb, as it may seem, it is fully consistent with the standard.
You can declare cycle iterators as volatile to force side effects. In this case, the loop should be executed even if it is pointless, since reading / writing to the volatile variable is a side effect that the compiler cannot optimize.
Please keep in mind that embedded system compilers often have a non-standard “minimum run” option when they skip initializing static storage duration variables to speed up system loading. If such a non-standard option is enabled, the variables will contain garbage values instead of zeros.
Lundin
source share