Undefined behavior and sequence point

Since the past few days, I have been trying to find out about undefined behavior . A few days ago I found the c-faq link. This helps a lot to eliminate a lot of confusion, but it creates another big confusion when I read question # 3.8 . After my many attempts to understand this expression (especially the second sentence);

The standard states that

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

I was better off asking this question , but none of the answers there explained the second sentence of this statement. Finally, I got an explanation about this. After reading it and frequently asked questions, I concluded that:

1. Last sentence

In addition, the previous value should only be accessed to determine the value to be stored.

will be like this:

In addition, the first value of the object should be accessed only to determine the value of the changed / new ( the same object ) to be saved.

As seen in the example

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

in the case of the expression i = i + 1 previous value (which is 1 here) i (in RHS) is available to determine the value of i to be stored. If, in the case of j = i + 1 and a[i] = i available value of i is simply the value not , as not, where i varies in these operators.

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

Between the previous and the next point in the sequence, the object must have a value that its stored value is changed no more than once by evaluating the expression.

could not be obtained , since i changes only once between two consecutive points of the sequence. And so we need a second sentence.
Both of these examples are forbidden in C, because the previous value of i was accessed twice, i.e. i++ itself accessed the previous value of i in the expression to change it and, therefore, another access to the previous value / value of i useless, since it is not accessed to determine the changed value that needs to be saved.

The problem starts when I came up with the expression i = i++ , which is mentioned in c-faq

Actually, the other expressions that we discussed also violate the second sentence.

I think that in this expression i (in RHS) access is available to determine the changed value of i .
How does this expression violate the second statement?

+4
source share
1 answer

Think of it this way:

 a = i++; 

is equivalent to:

 a = i; i++; 

Accessing the value of i in the increment has nothing to do with determining which value of a will be stored in a. So, i = i++ contains two modifications of i (which is prohibited by the first sentence), but also the modification i = for i does not depend on one of the calls to i in i++ .

I think someone was just smarter there. There is no need to determine how undefined behavior is undefined. It is enough to change the value twice undefined.

+3
source

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


All Articles