yjc
October 4, 2014, 2:54am
1
Hi,
[code]#include
int main(){
int i = 0, arr[8];
while(i<8){
arr[i++] = 2*i;
}
std::cout<<i<<“::::::”<<arr[i-2]<<std::endl;
return 0;
}[/code]
I run above code using root and g++, and I got different results.
and
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?
Thanks.
yjc
yjc
October 4, 2014, 6:45am
3
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.
yjc
October 4, 2014, 10:27am
5
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.