I understand that this is undefined behavior:
int i = 0;
int a[4];
a[i] = i++; //<--- UB here
because the order of evaluation of i
for the left hand side and the right hand side are undefined (the ;
is the only sequence point).
Taking that reasoning a step further it seems to me that this would be unspecified behavior:
int i = 0;
int foo(){
return i++;
}
int main(){
int a[4];
a[i] = foo();
return 0;
}
Even though there are a few sequence points on the right hand side of the =
as far as I understand it is still unspecified whether f()
or a[i]
is evaluated first.
Are my assumptions correct? Do I have to take painstaking care when I use a global or static variable on the left hand side of an assignment that the right hand does not under any circumstances modify it?
a
or the function call. – Compact