Strange difference between optimized/non optimized microsoft c++ code
- by Anders Forsgren
I have a c++ program with a method that looks something like this:
int myMethod(int* arr1, int* arr2, int* index)
{
arr1--;
arr2--;
int val = arr1[*index];
int val2 = arr2[val];
doMoreThings(val);
}
With optimizations enabled (/O2) the first line where the first pointer is decremented is not executed.
I assume the compiler believes that the arr1 array is not used since it thinks it can remove the decrement. Am I violating some convention in the above code? What could cause this behavior? It is a very old piece of f2c-translated code, the pointer decrement is due to the 1-based indexing of the original code.