Undefined behavior and sequence point

From past few days I was trying to learn about undefined behavior. Few days ago I found a c-faq link. This helps a lot to clear many confusions, but creates an another big confusion when I read the question #3.8. After my lots of efforts to understand the statement (specially second sentence);

The Standard states that

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

I felt better to ask this question on SO but none of the answer there explained the second sentence of this statement. Finally, I got an explanation there about this point . After reading it and FAQ many times I concluded that;

1.The last sentence

Furthermore, the prior value shall be accessed only to determine the value to be stored

would be like this;

Furthermore, the prior value of an object shall be accessed only to determine the modified/new value( of same object ) to be stored.

As it is clear by the example

 int i = 1, j, a[5];    
 i = i + 1;
 j = i + 1;
 a[i] = i; 

in case of expression i = i + 1 prior value (which is 1 here) of i (in RHS) is accessed to determine the value of i to be stored. While in case of j = i + 1 and a[i] = i , the accessed value of i is just value not prior value as no where i is modified in these statements.

2.In case of expression a[i] = i++ or a[i++] = i , first sentence of above statement

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.

get failed as i is modified only once between two consecutive sequence point. And that's why we need second sentence.
Both of these examples are disallowed in C because the prior value of i accessed two times ie, i++ itself access prior value of i in the expression to modify it and hence other access of prior value / value of i is needless as it is not accessed to determine the modified value to be stored.

The problem starts when I came up with the expression i = i++ about which it is stated in c-faq

Actually, the other expressions we've been discussing are in violation of the second sentence, as well.

I think in this expression i (in RHS) is accessed to determine the modified value of i .
How this expression is violating the second statement?


Think about it this way:

a = i++;

is equivalent to:

a = i;
i++;

The accesses the value of i in the increment has nothing to do with determining what value a will be stored into a. So i = i++ contains two modifications of i (which is disallowed by the first sentence), but also, the i = modification to i is independent from one of the accesses to i in i++ .

I think someone was just being extra clever there. There's no need to figure how much an undefined behavior is undefined. Modifying a value twice is enough undefined.

链接地址: http://www.djcxy.com/p/73194.html

上一篇: 在C ++ 11中,`i + = ++ i + 1`表现出未定义的行为?

下一篇: 未定义的行为和顺序点