Topic: Code does weird stuff depending on compilier optimization level used.
I am using version 2.21 build 1837.
I am testing a different version of my simulation. Note that the original simulation has been running stably for over a year. The new version is a reduced complexity of the original code somewhat and at the same time, reduced its memory footprint without really changing what it does. Note too that the older code uses the compiler options of "Extreme" optimization and has the "Aggressive Loop Optimization" box checked at compile time, so things are fully optimized. Note too that none of the testing below uses any debug level in the compile step.
I noted shortly after testing the new version that at some point deep into the simulation, that things start to get weird and by that I mean I get nonphysical results. I have over the last several weeks tried to figure out if I have some mistake in the code that causes this, but found nothing obvious; stared at the code until I was cross-eyed. When all else failed me, I wondered if the compiler options may have something to do with it. So I ran the code with no optimizations and it runs perfectly, with no anomalous behavior.
Then I decided to see which optimization level would cause it to happen. After days of testing, the anomalous behavior happens when the "Extreme" option is used.
Let me get more specific about what this anomalous behavior is. My code tracks positions and velocities of objects. After running for a significant time, the first object in my array of objects has its velocity in the Y-direction go in one direction and increase over time, accelerating the object away from where it should have been. Only the first object get this behavior, every other object behaves as the simulation physics dictates. Again, this does not happen in my older code.
As I have said before, I am not the world's best coder but I would like to understand what is happening here. Are there rookie mistakes in the code that can cause this during extreme optimization; what should I avoid?
Thanks in advance.
Rod