简体   繁体   中英

Isn't a[i++] = 1 (one) where, computation of the increment to be unsequenced relative to the indexing of the array, leading to violation of S6.5.2

Word limit on question length..

As pointed out by @Karl Knechtel I am confused that isn't fetching the operation of the array indexing unsequenced relative to the i++ increment operation? If they are unsequenced, why the C Standard 6.5.2 line mentioning about (emphasis added to the words/phrase which i understand, applies here)

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object , the behavior is undefined.

I read this question I can not understand some sentences in C99 wherein the OP tries to understand why a[i++] = 1 is undefined. Accepted and one of the highest voted answers by Pascal Cuoq mentions that this is defined behavior.

I also tried compiling the program using the -std=c99 , -Wall and -Wextra flag and a slew of other flags (basically all the flags which are enabled in GCC 11.2.0), but the code didn't throw any warning .

However, my question/confusion is why is this a defined behaviour?

From the C11 standard S6.5.2

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.If there are multiple allowable orderings of the subexpressions of an expression, the behaviour is undefined if such an unsequenced side effect occurs in any of the orderings.

my understanding/reasoning after reading through most of the threads on SO (with Tags [C] and [sequence-points] ) is that i++ would result in a side effect of updating the value of i. in that case this side-effect is unsequenced to the value computation using the same scaler object. I understand that a[integer object] constitutes value computation . Then, it should be undefined behavior?

Even from the C99 S6.5(p2)

Furthermore, the prior value shall be read only to determine the value to be stored.

I understand/construe that this expression should also render a[i++] = 1 undefined?

in that case this side-effect is unsequenced to the value computation using the same scaler object.

The scalar object involved in i++ is i . The side effect of updating i is not unsequenced relative to the computation of the value of i++ because C 2018 6.5.2.4 (which specifies behavior of postfix increment and decrement operators) paragraph 2 says:

… The value computation of the result is sequenced before the side effect of updating the stored value of the operand…

C 2011 has the same wording. (C 2018 contains only technical corrections and clarifications to C 2011.)

Even from the C99 S6.5(p2)

Furthermore, the prior value shall be read only to determine the value to be stored.

A rule in the C 1999 standards has no application to the 2011 or 2018 standards; it must be interpreted separately. Between 1999 and 2011, the standard moved from solitary sequence points to finer rules about sequencing relationships.

In i++ , the prior value is read to determine what the new value of i should be, so it conforms to that rule.

The rule was an attempt to say that any reads of a scalar object had to be in the prerequisite chain of an writes of the object. For example, in i = 3*i + i*i , all three reads of i are necessary to compute the value to be written to i , so they are necessarily performed before the write. But in i = ++i + i; , the read of i for that last term is not a prerequisite for writing to i for the ++i , so it is not necessarily performed before the write. Thus, it would not conform to the rule.

… I am confused that isn't fetching the operation of the array indexing unsequenced relative to the i++ increment operation?

The read of the array element is unsequenced relative to the update of i , and that is okay because there is no rule that requires it to be sequenced. C 2018 6.5 2 says, emphasis added:

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

The array element is a different scalar object from i , so we do not care that there is no sequencing between the read of the array element and the update to i .

Thanks a lot for the immediate responses from members. I would try to attempt an answer in the language, which, I understood.

After reading a suggestion to read this article use of abstract tree to tackle sequence point problem (I know, it's not normative)

Let me represent a[i++] =1 using the abstract syntax tree.

表达式 a[i++] = 1 的抽象树表示;

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM