The following code appeared in this question:
int k = 0;
while(+(+k--)!=0)
k=k++;
The statement k=k++
would unquestionably invoke undefined behavior if executed because k
is both read and written without a sequence point. However, in the above code the body of the loop is never entered so the offending statement is not executed.
The question is does this cause the whole program to be undefined anyway? In a situation such as this:
if (x != NULL)
*x = 4;
The statement *x = 4;
would only cause undefined behavior if x
is NULL. But in the case of k=k++
it is always undefined.
So the question is how would a compiler be expected to handle code such as this? Does it automatically invalidate the entire program, even if the statement doesn't execute?