But this thread is trending away from the purpose of me posting my question to the forum. I'm not asking for a code review, just how to overcome the optimization issues that are affecting my program. My snippet posts are just examples of what I observed while debugging my code. |
I think you may have misunderstood the significance of some of firedraco's points above. To return to code #1:
1 2 3 4 5
|
if (it != table.at(_index).end())
{
auto pos = distance(table.at(_index).begin(), it); // <--- "pos" is <optimized out>
table.at(_index).at(pos) = _tag;
}
|
Without any optimizations on the
auto pos
line one would expect the compiler to allocate memory for the variable pos and then initialize pos with the return value from the distance function which could be on the stack or even in a register (we neither know nor care; that's the compiler's problem). On the next line the
pos variable must be read and passed as a parameter to the
at function. When you turn optimizations on the compiler does not even need to allocate any memory for pos but treats the code as though you had written:
table.at(_index).at(distance(table.at(_index).begin(), it)) = _tag;
as firedraco showed above. The result is exactly the same. Note that there is nothing wrong with your original code and it is actually clearer than writing it all on a single line with all the nested function calls and it doesn't hurt performance since the compiler will optimize
pos away. That optimization is not the cause of your problem.
Now consider the following scenario:
Suppose elsewhere in your program you have an array and are accessing the array through a pointer, but your code has a logic error and your initialization of the pointer has a one-off error and eventually your pointer points one element past the end of array which is then dereferenced (undefined behavior). Further suppose that some other variable in your program is stored just past the end of the array, perhaps a variable like
pos.
When you run the program in debug mode you get one result, but when you run the program with optimizations turned on you get a different result because the variable that was stored just past the end of the array was optimized away or just because memory is laid out differently.
In getting different results in debug and with optimizations turned on you appear to be interpreting that outcome to mean:
"There's something wrong with the way the compiler does optimizations, so I need to know how to fix the problem with a switch or compiler setting."
But that is extremely unlikely to be the case. Instead, you should interpret what is happening as a warning that there is something wrong with your code. The example of reading past the end of an array was just to show how undefined behavior could lead to your problem. It could easily be that you are
relying on undefined behavior, (as firedraco pointed out above) i.e., you are using some construct that you think works a particular way, whereas the standard says the behavior of that construct is undefined.
But this thread is trending away from the purpose of me posting my question to the forum. I'm not asking for a code review, just how to overcome the optimization issues that are affecting my program. |
Unfortunately,a code review is probably what you are going to need to do.
Good luck.