By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

x=(x=5,11)

P: n/a
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?
May 27 '06 #1
Share this Question
Share on Google+
111 Replies


P: n/a
Jordan Abel wrote:
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

May 27 '06 #2

P: n/a
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
Jordan Abel wrote:
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?


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"

Robert Gamble

May 27 '06 #3

P: n/a
Jordan Abel wrote:
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
Jordan Abel wrote:
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?


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

May 27 '06 #4

P: n/a
Jordan Abel wrote:

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/>
May 27 '06 #5

P: n/a

Jordan Abel wrote:
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

May 27 '06 #6

P: n/a
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
Jordan Abel wrote:
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
> Jordan Abel wrote:
>> 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?
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


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?
May 27 '06 #7

P: n/a
On 2006-05-27, CBFalconer <cb********@yahoo.com> wrote:
Jordan Abel wrote:

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.


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.
May 27 '06 #8

P: n/a
CBFalconer wrote:
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
May 27 '06 #9

P: n/a
Jordan Abel wrote:

On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:

, 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?


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.

--
pete
May 27 '06 #10

P: n/a
CBFalconer <cb********@yahoo.com> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 27 '06 #11

P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 27 '06 #12

P: n/a
Jordan Abel wrote:
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
--
==============
Not a pedant
==============
May 27 '06 #13

P: n/a
Jordan Abel wrote:
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?

--
==============
Not a pedant
==============
May 27 '06 #14

P: n/a
pemo said:
Jordan Abel wrote:
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?


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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 27 '06 #15

P: n/a
Richard Heathfield wrote:

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.

--
pete
May 27 '06 #16

P: n/a
Richard Heathfield wrote:
pemo said:
Jordan Abel wrote:
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?


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?]!
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.


--
==============
Not a pedant
==============
May 27 '06 #17

P: n/a
Jordan Abel wrote:
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
Jordan Abel wrote:
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
> Jordan Abel wrote:
>> 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?

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


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.
, 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?


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

May 27 '06 #18

P: n/a
In article <11**********************@j33g2000cwa.googlegroups .com>,
Robert Gamble <rg*******@gmail.com> wrote:
>> > x=(x=5,11);
"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
May 27 '06 #19

P: n/a
Richard Tobin wrote:
In article <11**********************@j33g2000cwa.googlegroups .com>,
Robert Gamble <rg*******@gmail.com> wrote:
>> > x=(x=5,11);

"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.


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

May 27 '06 #20

P: n/a
On 2006-05-27, Jordan Abel <ra*******@gmail.com> wrote:
On 2006-05-27, CBFalconer <cb********@yahoo.com> wrote:
Jordan Abel wrote:

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.


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.


Once I used telnet to read clc, just to see if I understood NNTP well
enough. I piped the output through awk to make a hack custom newsreader.
I can't imagine any claim about newsreaders in the world being "outrageous".

I'm using slrn right now, and the subject is way at the top of my screen.
Reading it would be a pain.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 27 '06 #21

P: n/a
On 2006-05-27, pemo <us***********@gmail.com> wrote:
Jordan Abel wrote:
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


Why? The compiler doesn't need to emit code to evaluate it to know the value.

I think that the statement

char foo[9];
x=(x=sprintf(foo,"hello"),sprintf(foo," world!\n"));

could do things in this order:

x=8
x=5
set foo to "hello"
set foo to "world!\n"
May 27 '06 #22

P: n/a
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
Jordan Abel wrote:
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
> Jordan Abel wrote:
>> On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
>> > Jordan Abel wrote:
>> >> 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?
>>
>> 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
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.


And what part of the standard defines the behavior in this case?

You're introducing a sequence point between the right-hand side of the
assignment operator and the assignment itself. There is no such sequence
point mentioned in the standard. The fact that the standard _does_
define such a sequence point between the evaluation of the arguments to
a function [the right-hand side of the function call operator] and the
call itself, it would appear that if they meant for a sequence point to
be there, they would have said so.

Nowhere does it say that the semantics of the abstract machine include
finishing the evaluation of the right-hand side of the assignment, or
even starting it, before assigning the final value to the variable.
> , 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?


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?
If the right operand is a comma expression with an expression whose
value is known to be constant as its right-most part. See my printf
example in the other post.
Robert Gamble

May 27 '06 #23

P: n/a
On 2006-05-27, Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-05-27, Jordan Abel <ra*******@gmail.com> wrote:
On 2006-05-27, CBFalconer <cb********@yahoo.com> wrote:
Jordan Abel wrote:

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.


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.


Once I used telnet to read clc, just to see if I understood NNTP well
enough. I piped the output through awk to make a hack custom newsreader.
I can't imagine any claim about newsreaders in the world being "outrageous".

I'm using slrn right now, and the subject is way at the top of my screen.
Reading it would be a pain.


It's not at the top of the message? slrn displays a subset of headers
(including subject) in the message pane, then a blank line, then the
message body.
May 27 '06 #24

P: n/a

Jordan Abel wrote:
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?


The result of x=(x=5,11); is to set x to the value 11, with
no undefined behavior.

If x is volatile, there will be two stores to x, the first store
storing the value 5, the second store storing the value 11.

May 27 '06 #25

P: n/a

Richard Heathfield wrote:
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 ...


Yes it can, by not changing it at all. The behavior is defined, as
written.

May 27 '06 #26

P: n/a

Richard Tobin wrote:
In article <11**********************@j33g2000cwa.googlegroups .com>,
Robert Gamble <rg*******@gmail.com> wrote:
>> > x=(x=5,11);

"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.


No, it isn't, because of the sequence point rule for the comma
operator.

May 27 '06 #27

P: n/a

Jordan Abel wrote:
On 2006-05-27, pemo <us***********@gmail.com> wrote:
Jordan Abel wrote:
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


Why? The compiler doesn't need to emit code to evaluate it to know the value.

I think that the statement

char foo[9];
x=(x=sprintf(foo,"hello"),sprintf(foo," world!\n"));

could do things in this order:

x=8
x=5
set foo to "hello"
set foo to "world!\n"


You're falling into the trap of arguing what a compiler
might do rather than what a compiler is obliged to do
by the Standard.

The Standard requires that the evaluation of x=sprintf(foo,"hello")
precede the evaluation of sprintf(foo," world!\n"), and that the
evaluation of sprintf(foo," world!\n") precede the assignment
of 8 to x, because it is evaluations that produce values.

May 27 '06 #28

P: n/a
Keith Thompson wrote:
CBFalconer <cb********@yahoo.com> writes:
[...]
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.


My newsreader has the heading part configurable. If I were to have it
set to not display any header (or not the Subject part) then I'd have
to look up into the message tree.

At any rate, it's usenet standard to not rely on people reading the
subject line to figure out the post.

Brian

May 27 '06 #29

P: n/a

Jordan Abel wrote:
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
Jordan Abel wrote:
On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
> Jordan Abel wrote:
>> On 2006-05-27, Robert Gamble <rg*******@gmail.com> wrote:
>> > Jordan Abel wrote:
>> >> 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?
>>
>> 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

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.


And what part of the standard defines the behavior in this case?

You're introducing a sequence point between the right-hand side of the
assignment operator and the assignment itself. ...


Actually, he isn't. Try reading more carefully.
Nowhere does it say that the semantics of the abstract machine include
finishing the evaluation of the right-hand side of the assignment, or
even starting it, before assigning the final value to the variable.


The only way to get a value of out of (x=5,11) is to evaluate the
comma operator per its Semantics. Read section 6.5.17.

May 27 '06 #30

P: n/a
On 2006-05-27, en******@yahoo.com <en******@yahoo.com> wrote:

Richard Tobin wrote:
In article <11**********************@j33g2000cwa.googlegroups .com>,
Robert Gamble <rg*******@gmail.com> wrote:
>> >> > x=(x=5,11);

>"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.


No, it isn't, because of the sequence point rule for the comma
operator.


Repeat after me:

Sequence points define a partial ordering.
May 27 '06 #31

P: n/a
On 2006-05-27, en******@yahoo.com <en******@yahoo.com> wrote:

Jordan Abel wrote:
On 2006-05-27, pemo <us***********@gmail.com> wrote:
> Jordan Abel wrote:
>> 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
Why? The compiler doesn't need to emit code to evaluate it to know the value.

I think that the statement

char foo[9];
x=(x=sprintf(foo,"hello"),sprintf(foo," world!\n"));

could do things in this order:

x=8
x=5
set foo to "hello"
set foo to "world!\n"


You're falling into the trap of arguing what a compiler
might do rather than what a compiler is obliged to do
by the Standard.


The "as if" rule applies.

The Standard requires that the evaluation of x=sprintf(foo,"hello")
precede the evaluation of sprintf(foo," world!\n"), and that the
evaluation of sprintf(foo," world!\n") precede the assignment
of 8 to x, because it is evaluations that produce values.

May 27 '06 #32

P: n/a
Jordan Abel wrote:
Repeat after me:

Sequence points define a partial ordering.


What I came away with, from the "p = p -> next = q" debate,
was a general principle of avoiding expressions
with multiple assignments involving the same variable.
x=(x=5,11) is one of those.
I think you're correct.
Good luck with your attempts to persuade.

--
pete
May 28 '06 #33

P: n/a
On 27 May 2006 13:49:50 -0700,
en******@yahoo.com <en******@yahoo.com> wrote:

Jordan Abel wrote:
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?


The result of x=(x=5,11); is to set x to the value 11, with
no undefined behavior.

If x is volatile, there will be two stores to x, the first store
storing the value 5, the second store storing the value 11.

Well I disagree. The side effects of x=5 must be evaluated before the
side effects of 11, but nothing requires the side effects of the x=()
assignment to be evaluated after the side effects of the x=5
assignment.

Tim.

--
God said, "div D = rho, div B = 0, curl E = - @B/@t, curl H = J + @D/@t,"
and there was light.

http://tjw.hn.org/ http://www.locofungus.btinternet.co.uk/
May 28 '06 #34

P: n/a
Jordan Abel wrote:
[x=(x=5,11)]
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?


To use another example:

#include <stdlib.h>
int main(void) {
int *p = 0;
exit(0), *p;
}

Is this a valid C program?

I believe there is no significant difference between this, and
x=(x=5,11). In both cases, the behaviour is defined if and only if the
right operand of the , operator is not ever evaluated before the left
operand is, in both cases there are no side effects in the evaluation
of the right operand, and in both cases and the question is whether the
as-if rule can introduce undefined behaviour when there would otherwise
not be any. Do you agree that this example has the same problem? And if
not, why not? (The reason I'm using this example is because disallowing
it is much more surprising to me than disallowing your original code.)

May 28 '06 #35

P: n/a
On 2006-05-27, Jordan Abel <ra*******@gmail.com> wrote:
On 2006-05-27, Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-05-27, Jordan Abel <ra*******@gmail.com> wrote:
On 2006-05-27, CBFalconer <cb********@yahoo.com> wrote:
Jordan Abel wrote:
>
> 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.

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.


Once I used telnet to read clc, just to see if I understood NNTP well
enough. I piped the output through awk to make a hack custom newsreader.
I can't imagine any claim about newsreaders in the world being "outrageous".

I'm using slrn right now, and the subject is way at the top of my screen.
Reading it would be a pain.


It's not at the top of the message? slrn displays a subset of headers
(including subject) in the message pane, then a blank line, then the
message body.


Not once you scroll down, and because I'm usually SSHed onto this machine
over a dial-up equivilant connection, scrolling back up is not a worthy
use of time.

This, however, is complete OT to standard C. You can have the last word
if you want.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 28 '06 #36

P: n/a
Robert Gamble wrote:

Jordan Abel wrote:

[...]
> > x=(x=5,11);
[...] = 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?


Well, "the value of the right operand" can be determined without
necessarily introducing all of the side effects. Extending that
logic, the following should be "defined" as well:

x = (x++,11);

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

May 28 '06 #37

P: n/a
Kenneth Brody wrote:
Robert Gamble wrote:

Jordan Abel wrote:

[...]
>> > x=(x=5,11); [...] = 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?


Well, "the value of the right operand" can be determined without
necessarily introducing all of the side effects. Extending that
logic, the following should be "defined" as well:

x = (x++,11);


Right, and it is.

Robert Gamble

May 28 '06 #38

P: n/a

Jordan Abel wrote:
On 2006-05-27, en******@yahoo.com <en******@yahoo.com> wrote:

Richard Tobin wrote:
In article <11**********************@j33g2000cwa.googlegroups .com>,
Robert Gamble <rg*******@gmail.com> wrote:

>> >> > x=(x=5,11);

>"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.


No, it isn't, because of the sequence point rule for the comma
operator.


Repeat after me:

Sequence points define a partial ordering.


You seem to think that sequence points are the only thing
that affect the partial ordering of expression evaluation.
That's false. In the expression a + b - c, the evaluation
of + must precede the evaluation of - in the abstract
machine. And compiled code must behave as if
the abstract machine would behave.

May 28 '06 #39

P: n/a

Tim Woodall wrote:
On 27 May 2006 13:49:50 -0700,
en******@yahoo.com <en******@yahoo.com> wrote:

Jordan Abel wrote:
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?


The result of x=(x=5,11); is to set x to the value 11, with
no undefined behavior.

If x is volatile, there will be two stores to x, the first store
storing the value 5, the second store storing the value 11.

Well I disagree. The side effects of x=5 must be evaluated before the
side effects of 11, but nothing requires the side effects of the x=()
assignment to be evaluated after the side effects of the x=5
assignment.


You're welcome to your opinion, but don't expect to convince
anyone without backing it up with language from the Standard.

In fact, the Standard does require that an expression be evaluated
before its value is produced, as I have explained at length in a
previous thread.

May 28 '06 #40

P: n/a
On 28 May 2006 13:34:28 -0700,
en******@yahoo.com <en******@yahoo.com> wrote:

Jordan Abel wrote:
On 2006-05-27, en******@yahoo.com <en******@yahoo.com> wrote:
>
> Richard Tobin wrote:
>> In article <11**********************@j33g2000cwa.googlegroups .com>,
>> Robert Gamble <rg*******@gmail.com> wrote:
>>
>> >> >> > x=(x=5,11);
>>
>> >"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.
>
> No, it isn't, because of the sequence point rule for the comma
> operator.


Repeat after me:

Sequence points define a partial ordering.


You seem to think that sequence points are the only thing
that affect the partial ordering of expression evaluation.
That's false. In the expression a + b - c, the evaluation
of + must precede the evaluation of - in the abstract
machine. And compiled code must behave as if
the abstract machine would behave.


So you think

x ^= y ^= x ^= y;

has defined behaviour? After all, just as +/- associates L to R, ^=
associates R to L.

Tim.

--
God said, "div D = rho, div B = 0, curl E = - @B/@t, curl H = J + @D/@t,"
and there was light.

http://tjw.hn.org/ http://www.locofungus.btinternet.co.uk/
May 28 '06 #41

P: n/a
en******@yahoo.com said:
In the expression a + b - c, the evaluation of + must precede the
evaluation of - in the abstract machine.


C99, 6.5(3): 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.

In the expression a + b - c, the abstract machine is under no obligation to
evaluate + before - or vice versa. Indeed, even a, b, and c themselves
might be evaluated in any order.

To bring this back into the context of the thread topic, it is clear that
the onus is on the programmer to ensure that the result produced is the
result the programmer expected - and the way to do that is to write clear,
simple code that removes the possibility of undefined behaviour, without
being forced to rely on en*****@yahoo.com's assurance that this possibility
does not exist.

In this case, this translates to replacing:

x=(x=5,11);

with:

x = 11;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 28 '06 #42

P: n/a
2006-05-28 <<11**********************@j55g2000cwa.googlegroup s.com>>, en******@yahoo.com wrote:

Jordan Abel wrote:
On 2006-05-27, en******@yahoo.com <en******@yahoo.com> wrote:
>
> Jordan Abel wrote:
>> On 2006-05-27, pemo <us***********@gmail.com> wrote:
>> > Jordan Abel wrote:
>> >> 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
>>
>> Why? The compiler doesn't need to emit code to evaluate it to know the value.
>>
>> I think that the statement
>>
>> char foo[9];
>> x=(x=sprintf(foo,"hello"),sprintf(foo," world!\n"));
>>
>> could do things in this order:
>>
>> x=8
>> x=5
>> set foo to "hello"
>> set foo to "world!\n"
>
> You're falling into the trap of arguing what a compiler
> might do rather than what a compiler is obliged to do
> by the Standard.


The "as if" rule applies.


Yes, and the code you posted doesn't behave as if
it were executed by the abstract machine.


The behavior of the abstract machine is not defined by the standard in
this case.
May 28 '06 #43

P: n/a

Jordan Abel wrote:
On 2006-05-27, en******@yahoo.com <en******@yahoo.com> wrote:

Jordan Abel wrote:
On 2006-05-27, pemo <us***********@gmail.com> wrote:
> Jordan Abel wrote:
>> 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

Why? The compiler doesn't need to emit code to evaluate it to know the value.

I think that the statement

char foo[9];
x=(x=sprintf(foo,"hello"),sprintf(foo," world!\n"));

could do things in this order:

x=8
x=5
set foo to "hello"
set foo to "world!\n"


You're falling into the trap of arguing what a compiler
might do rather than what a compiler is obliged to do
by the Standard.


The "as if" rule applies.


Yes, and the code you posted doesn't behave as if
it were executed by the abstract machine.

May 28 '06 #44

P: n/a
2006-05-28 <<11**********************@i40g2000cwc.googlegroup s.com>>, Harald van D?k wrote:
Jordan Abel wrote:
[x=(x=5,11)]
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?


To use another example:

#include <stdlib.h>
int main(void) {
int *p = 0;
exit(0), *p;
}

Is this a valid C program?

I believe there is no significant difference between this, and
x=(x=5,11). In both cases, the behaviour is defined if and only if the
right operand of the , operator is not ever evaluated before the left
operand is, in both cases there are no side effects in the evaluation
of the right operand, and in both cases and the question is whether the
as-if rule can introduce undefined behaviour when there would otherwise
not be any. Do you agree that this example has the same problem? And if
not, why not? (The reason I'm using this example is because disallowing
it is much more surprising to me than disallowing your original code.)


There is no part of the expression x=(x=5,11) that is not reached.

This does seem a lot like the p=p->next=q discussion, which i'd
forgotten until now, and which I don't think we ever came to a consensus
on.
May 28 '06 #45

P: n/a
Richard Heathfield wrote:
en******@yahoo.com said:
In the expression a + b - c, the evaluation of + must precede the
evaluation of - in the abstract machine.


In the expression a + b - c, the abstract machine is under no obligation to
evaluate + before - or vice versa. Indeed, even a, b, and c themselves
might be evaluated in any order.


I disagree. If the expression is 5 + 6 - 3, then the machine must
compute 5 + 6 before it subtracts 3 (notwithstanding the as-if rule).

I'm not sure what other possibility you are trying to allow for here?

May 28 '06 #46

P: n/a
Old Wolf said:
Richard Heathfield wrote:
en******@yahoo.com said:
> In the expression a + b - c, the evaluation of + must precede the
> evaluation of - in the abstract machine.


In the expression a + b - c, the abstract machine is under no obligation
to evaluate + before - or vice versa. Indeed, even a, b, and c themselves
might be evaluated in any order.


I disagree. If the expression is 5 + 6 - 3, then the machine must
compute 5 + 6 before it subtracts 3 (notwithstanding the as-if rule).


Please explain your reasoning in the light of 6.5(3), which I cited
upthread.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 28 '06 #47

P: n/a
Jordan Abel wrote:

There is no part of the expression x=(x=5,11) that is not reached.


Jordan, in all your posts on this thread you seem to ignore
the fact that the comma operator introduces a sequence point.

The rules of C are that nothing after a sequence point can
be evaluated until all side-effects from expressions prior to
the sequence point have been evaluated.

It is not possible to evaluate "11" before the assignment of
5 to x has completed.

It is not possible to assign 11 to x, before 11 has been evaluated.

By the logic that you are using, there might as well be no
sequence points; for example you would have to say that:

x = 1;
x = 2;

causes undefined behaviour because before 1 is assigned
to x, the compiler can "see" that 2 is going to be assigned
to x.

May 28 '06 #48

P: n/a
2006-05-28 <11**********************@i40g2000cwc.googlegroups .com>, Old Wolf wrote:
Jordan Abel wrote:

There is no part of the expression x=(x=5,11) that is not reached.


Jordan, in all your posts on this thread you seem to ignore
the fact that the comma operator introduces a sequence point.


Sequence points are fine, as far as they go, but you're forgetting that
they define a partial ordering.
May 28 '06 #49

P: n/a
Richard Heathfield wrote:
en******@yahoo.com said:
In the expression a + b - c, the evaluation of + must precede the
evaluation of - in the abstract machine.
C99, 6.5(3): 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.


This doesn't address the issue.
In the expression a + b - c, the abstract machine is under no obligation to
evaluate + before - or vice versa. Indeed, even a, b, and c themselves
might be evaluated in any order.


a, b, and c may be evaluated in any order, but the code *must* behave
as if the addition is done before the subtraction. The subtraction may
be done before the addition if and only if in every case, the same
result is generated, because otherwise the as-if rule doesn't apply.
(See also 5.1.2.3#14.)

May 29 '06 #50

111 Replies

This discussion thread is closed

Replies have been disabled for this discussion.