Strange difference between optimized/non optimized microsoft c++ code

Posted by Anders Forsgren on Stack Overflow See other posts from Stack Overflow or by Anders Forsgren
Published on 2012-06-05T10:37:15Z Indexed on 2012/06/05 10:40 UTC
Read the original article Hit count: 198

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c