x=(x=5,11)

J

Jordan Abel

Is this defined or not? Some people in ##c are saying that it has to
result in x being set to 11, i'm saying it's undefined. Who's right?
 
R

Robert Gamble

Jordan said:
Is this defined or not? Some people in ##c are saying that it has to
result in x being set to 11, i'm saying it's undefined. Who's right?

Here is the operation in question copied from the subject line:
x=(x=5,11);
It looks defined to me, there is a sequence point between the
assignments. What is your argument for it being undefined?

Robert Gamble
 
J

Jordan Abel

Here is the operation in question copied from the subject line:
x=(x=5,11);
It looks defined to me, there is a sequence point between the
assignments. What is your argument for it being undefined?


Robert Gamble
 
R

Robert Gamble

Jordan said:
How is there a sequence point between "x=5" and "x=<inner_expression>"?
the only sequence point i see is between "x=5" and "11"

The RHS of the expression must be evaluated to determine the value to
store in x, in that expression there is a sequence point after which x
is not modifed before the value of the subexpression is stored into x.

Robert Gamble
 
C

CBFalconer

Jordan said:
Is this defined or not? Some people in ##c are saying that it has to
result in x being set to 11, i'm saying it's undefined. Who's right?

I have no idea what ##c means. However if you are (and you should
be) talking about the C language, then they are right.

BTW don't rely on the subject being visible when reading the
article. It often isn't. Google is not usenet.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
F

Frodo Baggins

Jordan said:
Is this defined or not? Some people in ##c are saying that it has to
result in x being set to 11, i'm saying it's undefined. Who's right?

I am not sure, but comma has lower precedence than assignment.
hence the assignment x=5 occurs then due to comma, 11 is the value of
the expression
x=5,11 and this is assigned to x.

However if I remove the parantheses, gcc gives a warning that the
operation on x may be undefined.

Also assignment operator does not define a sequence point but the end
of each operand of the comma operator does.

Regards,
Frodo Baggins
 
J

Jordan Abel

The RHS of the expression must be evaluated to determine the value to
store in x

Says who? The compiler already knows the final value of the expression.
, in that expression there is a sequence point after which x
is not modifed before the value of the subexpression is stored into x.

= is not a sequence point. By what authority do you claim that one thing
"must" be evaluated before another without a sequence point?
 
J

Jordan Abel

I have no idea what ##c means. However if you are (and you should
be) talking about the C language, then they are right.

BTW don't rely on the subject being visible when reading the
article. It often isn't. Google is not usenet.

Check my headers before accusing me of using google.

On what newsreaders is it not visible? It comes with it along with the
rest of the headers in the NNTP protocol, and any newsreader i've ever
seen has a titlebar or a thread display or displays a subset of the
headers along with the article text.

Mozilla 4.75 [en] (Win98; U) does the last two of those three. It may
also put the subject in the titlebar, i can't tell from the screenshots
i've found (you certainly seemed to identify the subject line well
enough.) Were you just being difficult? Maybe you should have been
prepared with an example of a newsreader that doesn't display the
subject, instead of assuming I was some google-using moron who would
blindly accept any outrageous claim about what newsreaders exist in the
world.
 
D

Default User

CBFalconer said:
BTW don't rely on the subject being visible when reading the
article. It often isn't. Google is not usenet.

Well, as his headers indicate:

User-Agent: slrn/0.9.8.1 (FreeBSD)


I don't think Google enters into it.




Brian
 
P

pete

Jordan said:
= is not a sequence point.
By what authority do you claim that one thing
"must" be evaluated before another without a sequence point?

I'm starting to see this, Jordan Abel's way.

6.5 Expressions
[#3] The grouping of operators and operands is indicated by
the syntax.61) Except as specified later (for the function-
call (), &&, ||, ?:, and comma operators), the order of
evaluation of subexpressions and the order in which side
effects take place are both unspecified.

The assignment expression itself, is a sequence point,
but the operands of the assignment operator are subexpressions.
 
K

Keith Thompson

CBFalconer said:
BTW don't rely on the subject being visible when reading the
article. It often isn't. Google is not usenet.

I've never heard of a newsreader that doesn't display the subject when
displaying the article (which doesn't, of course, mean that such a
newsreader doesn't exist). But regardless of that, it's certainly a
good idea to put the question in the body of the message, not just in
the subject header.
 
R

Richard Heathfield

Jordan Abel said:
Is [the expression x=(x=5,11)] defined or not? Some people in ##c
are saying that it has to result in x being set to 11, i'm saying
it's undefined. Who's right?

I would lean towards its being undefined, but I would not be so terribly
surprised if the Battle of the Language Lawyers ended with victory for the
opposing view.

My own take on the subject is rather pragmatic, I'm afraid. The code can
trivially be rewritten to remove the possibility of undefined behaviour and
to improve clarity, and I would recommend doing this at the earliest
possibility.

In this respect, it's similar to the p = p->next = q; debate that we had a
few years ago.
 
P

pemo

Jordan said:
Is this defined or not? Some people in ##c are saying that it has to
result in x being set to 11, i'm saying it's undefined. Who's right?

x=(X=5,11)

My reading [but what do I know!]

X=paraen'ed-expression

So, paraen'ed-expression must be evaluated first

(X=5,11)

X is assigned the value 5, and then the comma comes into play, and the
resulting value of the paraen'ed-expression is 11.

X=11
 
P

pemo

Jordan said:
Is this defined or not? Some people in ##c are saying that it has to
result in x being set to 11, i'm saying it's undefined. Who's right?

Where/what is ##c, I'd be interested in seeing the original conversation?
 
R

Richard Heathfield

pemo said:
Where/what is ##c, I'd be interested in seeing the original conversation?

No chance of that, unless someone kept logs. (Which, indeed, I might have
done! I don't know, but I could look if you care enough.)

He's talking about Freenode's C channel. Any sensible IRC network would call
it #c, so naturally Freenode adds another #, to make it ##c. Curiously,
#foo channels are reserved for those who actually have some kind of
"ownership" of <foo>. So #c is reserved for Dennis Ritchie, I guess, and
possibly the ISO guys. I'm sure they are very grateful. When this change
took effect, however, quite a few of those ejected from #c to make room for
Dennis were, shall we say, rather less grateful - and remain so.

Undernet's #c channel continues to have the more traditional name.
 
P

pete

Richard said:
Jordan Abel said:
Is [the expression x=(x=5,11)] defined or not? Some people in ##c
are saying that it has to result in x being set to 11, i'm saying
it's undefined. Who's right?

I would lean towards its being undefined,
but I would not be so terribly
surprised if the Battle of the Language Lawyers
ended with victory for the opposing view.

My own take on the subject is rather pragmatic, I'm afraid.
The code can trivially be rewritten
to remove the possibility of undefined behaviour and
to improve clarity, and I would recommend
doing this at the earliest possibility.

In this respect, it's similar to the p = p->next = q;
debate that we had a few years ago.

I recall that everyone who said that
p = p->next = q;
was defined,
also said that they wouldn't write code that way.
 
P

pemo

Richard said:
pemo said:


No chance of that, unless someone kept logs. (Which, indeed, I might
have done! I don't know, but I could look if you care enough.)

Ah, thanks Richard, presumably that's why Google has no record of it [it
being a realtime chat kind of thing]!

##c - what with c#, I thought this was some kind of new bastardized C
variant [deviant?]!
 
R

Robert Gamble

Jordan said:
Says who? The compiler already knows the final value of the expression.

A conforming implementation cannot introduce undefined behavior when
the semantics of the abstract machine are defined as they are in this
case. If the compiler wants to optimize the expression it must do so
without invoking undefined behavior.
= is not a sequence point. By what authority do you claim that one thing
"must" be evaluated before another without a sequence point?

6.5.16.1p2:
"In simple assignment (=), the value of the right operand is converted
to the type of the
assignment expression and replaces the value stored in the object
designated by the left
operand."

Now how is it possible to obtain the value of the right operand and
convert it to the type of the assignment expression without evaluting
it before you store the result in x?

Robert Gamble
 
R

Richard Tobin

[/QUOTE]
"In simple assignment (=), the value of the right operand is
converted to the type of the assignment expression and replaces the
value stored in the object designated by the left operand."
Now how is it possible to obtain the value of the right operand and
convert it to the type of the assignment expression without evaluting
it before you store the result in x?

Clearly it's not possible to store the result before evaluating it,
but it's possible (indeed, easy!) to determine the value of (x=5,11)
before performing the assignment of 5 to x. So the text you quote
does not preclude the assignment of 5 to x happening after the
assignment of 11 to x.

-- Richard
 
R

Robert Gamble

"In simple assignment (=), the value of the right operand is
converted to the type of the assignment expression and replaces the
value stored in the object designated by the left operand."
Now how is it possible to obtain the value of the right operand and
convert it to the type of the assignment expression without evaluting
it before you store the result in x?

Clearly it's not possible to store the result before evaluating it,
but it's possible (indeed, easy!) to determine the value of (x=5,11)
before performing the assignment of 5 to x. So the text you quote
does not preclude the assignment of 5 to x happening after the
assignment of 11 to x.[/QUOTE]

Right, I was speaking abstractly. The compiler is still bound by the
semantics of the abstract machine defined by the Standard, it can
perform any optimizations it likes but still must behave "as if" the
the semantics of the abstract machine were honored. In this case that
could mean reducing the expression to x = 11 (given no volatile
qualifier for x) but it cannot introduce undefined behavior in the
process. My point is that following the semantics outlined in the
Standard requires evaluating the RHS first and that any optimizations
must behave "as if" this were actually done.

Robert Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top