Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

c - Is assignment x=1; always an undefined behaviour according to C17?

I'm looking at the final draft of C17, N2176. Here, I'm concerned with what kind of expression with side effects would have it's behaviour undefined.

In section 6.5 Expressions of the standard, there is paragraph 2 that starts with:

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.

As I understand it, evaluation of expression x=1 would both produce a value and also initiate a side effect changing the value of object designated by x. The determining factor would than be whether the side effect is sequenced in any way in relation to the value computation that uses the value of object designated by x.

The description in section 6.5.16 Assignment operators contains this sentence:

The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands.

That does not resolve sequencing of value computation of the whole assignment and the side effect of the assignment.

Also, another sentence:

An assignment expression has the value of the left operand after the assignment, but is not an lvalue.

Specifies what the final value should be, but does not mandate any sequencing. And I don't see any other text mentioning sequencing regarding side effect and value.

I know that when written as a full statement x=1; the value of assignment is not used. However, the standard says that the value is discarded. That means that it is as if first the value was evaluated and later discarded, so the undefined behaviour should still be triggered.

Is there any other part of the standard that makes this statement behaviour not undefined?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The value of the assignment expression is not a use of the assigned object. This is because the value of x = 1, in, say y = x = 1, is not obtained by lvalue conversion of x but is a value computation of the assignment expression, per C 2018 6.5.16 3:

… An assignment expression has the value of the left operand after the assignment,…

It further says:

… The type of an assignment expression is the type the left operand would have after lvalue conversion…

This is about type, rather than value, but its use of the subjunctive “would have” indicates that “lvalue conversion” is fictitious; it does not actually occur. So an assignment expression is not obtaining its value by reading the left operand; its value is merely a result of the operation.

There is a footnote there (115) that says:

The implementation is permitted to read the object to determine the value but is not required to, even when the object has volatile-qualified type.

This tells us that the left operand may in fact be used to compute the value of the assignment expression. However, this is a statement that it may be implemented by doing so, not a statement that this is part of the semantics of assignment expressions in the C model of computing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...