Different results between root5.34/07 and g++4.9.1



int main(){
int i = 0, arr[8];
arr[i++] = 2*i;
return 0;

I run above code using root and g++, and I got different results.


I guess it is because root5.34/07 and (default)g++4.9.1 handle the index of a array differently. But I am not sure. Could someone explain it to me?



arr[i] = 2*i; i++;

I know this will produce the same result. But what confuses me is arr[i] and arr[i++] in root and g++.

You are not allowed to mix “i”, “i++”, “i–”, “++i”, “–i” in one expression. The compiler is totally free to choose in which order they will be evaluated (and it may depend on the optimization level, too) -> if you mix them, different compilers will often give different results.

I see. Thank you for your help.