I can not understand some sentences in C99

C99 6.5 says:

Between the previous and the next point in the sequence, the object must have its stored value, changed no more than once by evaluating the expression. In addition, the previous value should only be read to determine the value to be stored.

What does “In addition, it means that the previous value should be read only to determine the value that needs to be saved”? In C99, why a[i++] = 1 - undefined behavior?

+6
source share
2 answers

a[i++] = 1 (if it has no other reason to be undefined than a sequence of side effects: out of bound access or uninitialized i ).

You mean a[i++] = i , which is undefined because it reads i between the same points in the sequence as i++ that change it.

“Moreover, the previous value should only be read to determine the value to be saved” means that i = i + 1; allowed, although it reads from i and modifies i .

On the other hand, a[i] = (i=1); not allowed because, despite writing to i only once, reading from i not intended to calculate the stored value.

+8
source

"The preliminary value shall only be read to determine the stored value." It is clear that the wording is contradictory; why does the purpose for which the meaning is read matter?

The point of this proposal is the imposition of a requirement for which the results depend on which operations.

I will steal examples from the Pascal answer .

It:

 i = i + 1; 

excellent. i read and written in the same expression without an intermediate point in the sequence, but this is normal because writing cannot occur until the reading is complete. The value that needs to be saved cannot be calculated until the expression i + 1 and its subexpression i have been fully evaluated. (And i + 1 has no side effects that can be delayed until the time of writing.) This dependency imposes a strict order: the reading must be completed before recording begins.

On the other hand, these are:

 a[i] = (i=1); 

has undefined behavior. The subexpression a[i] reads the value of i , and the subexpression i=1 writes the value of i . But the value that should be written to i by writing does not depend on the score that reads i on the left side, and therefore the order of reading and writing is not defined. "Storage value" - 1 ; reading i in a[i] does not determine this value.

I suspect this confusion is that the 2011 revision of ISO C (available in draft N1570 ) reformulated this section. The standard still has the concept of sequence points, but 6.5p2 now says:

If the side effect of a scalar object is independent of another side effect on the same scalar object or the value of the calculation using the value of the same scalar object, the behavior is undefined. If there are several valid orders of expression subexpression, the behavior is undefined if such an Inconsistent side effect occurs in any of the orders.

And paragraph 1 explicitly states what was implicitly accepted on C99:

The calculation of the values ​​of the operands of the operator is sequenced before calculating the value of the result of the operator.

Section 5.1.2.3, paragraph 2, explains the sequence and sequence after the relationship.

+8
source

Source: https://habr.com/ru/post/954697/


All Articles