Question

I don't know where to search for this (probably the standard but still don't know what to search for), so I will ask this here.

If in some execution array[i2] will be set to array[i] where i2 happens to be equal to i, then is this defined behavior?

I'm using C99 (with gcc 4.8.1), looking at the assembly with gcc -S, I don't see anything suspicious.

Was it helpful?

Solution 3

If variable i has valid value then assignment

i = i;

has perfectly defined behavior. This is equivalent to your question, as stated.

If there's something more tricky in your situation, you have to provide more details about it.

OTHER TIPS

This is absolutely defined behaviour. The right-hand-side of an assignment is calculated first, then assigned to the left-hand-side. Note that the left-hand-side must resolve to an lvalue.

foo() {
  int i, j, a[5], b[5];
  i = i; // undefined because reading i is UB
  j = i; // undefined because reading i is UB
  a[i] = a[i]; // undefined because reading i is UB
  a[3] = a[3]; // undefined because reading a[3] is UB
  b[3] = a[3]; // undefined because reading a[3] is UB
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top