niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
x=(x=5,11)
Post new topic   Reply to topic Page 3 of 8 [112 Posts] View previous topic :: View next topic
Goto page:  Previous  1, 2, 3, 4, 5, 6, 7, 8 Next
Author Message
Jordan Abel
*nix forums Guru


Joined: 25 Oct 2005
Posts: 1366

PostPosted: Sat May 27, 2006 9:45 pm    Post subject: Re: x=(x=5,11) Reply with quote

On 2006-05-27, ena8t8si@yahoo.com <ena8t8si@yahoo.com> wrote:
Quote:

Richard Tobin wrote:
In article <1148734636.495897.218270@j33g2000cwa.googlegroups.com>,
Robert Gamble <rgamble99@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.
Back to top
Jordan Abel
*nix forums Guru


Joined: 25 Oct 2005
Posts: 1366

PostPosted: Sat May 27, 2006 9:46 pm    Post subject: Re: x=(x=5,11) Reply with quote

On 2006-05-27, ena8t8si@yahoo.com <ena8t8si@yahoo.com> wrote:
Quote:

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

Quote:

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.
Back to top
pete
*nix forums Guru


Joined: 09 Apr 2005
Posts: 1757

PostPosted: Sun May 28, 2006 1:37 am    Post subject: Re: x=(x=5,11) Reply with quote

Jordan Abel wrote:

Quote:
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
Back to top
Tim Woodall
*nix forums beginner


Joined: 16 Jul 2005
Posts: 13

PostPosted: Sun May 28, 2006 11:18 am    Post subject: Re: x=(x=5,11) Reply with quote

On 27 May 2006 13:49:50 -0700,
ena8t8si@yahoo.com <ena8t8si@yahoo.com> wrote:
Quote:

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/
Back to top
Harald van Dijk
*nix forums Guru Wannabe


Joined: 06 Feb 2006
Posts: 123

PostPosted: Sun May 28, 2006 2:38 pm    Post subject: Re: x=(x=5,11) Reply with quote

Jordan Abel wrote:
[x=(x=5,11)]
Quote:
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.)
Back to top
Kenneth Brody
*nix forums Guru


Joined: 20 Apr 2005
Posts: 401

PostPosted: Sun May 28, 2006 3:07 pm    Post subject: Re: x=(x=5,11) Reply with quote

Robert Gamble wrote:
Quote:

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:ThisIsASpamTrap@gmail.com>
Back to top
Andrew Poelstra
*nix forums Guru


Joined: 05 Nov 2005
Posts: 345

PostPosted: Sun May 28, 2006 3:13 pm    Post subject: Re: x=(x=5,11) Reply with quote

On 2006-05-27, Jordan Abel <random832@gmail.com> wrote:
Quote:
On 2006-05-27, Andrew Poelstra <apoelstra@localhost.localdomain> wrote:
On 2006-05-27, Jordan Abel <random832@gmail.com> wrote:
On 2006-05-27, CBFalconer <cbfalconer@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.
Back to top
Robert Gamble
*nix forums Guru


Joined: 15 Apr 2005
Posts: 447

PostPosted: Sun May 28, 2006 4:36 pm    Post subject: Re: x=(x=5,11) Reply with quote

Kenneth Brody wrote:
Quote:
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
Back to top
ena8t8si@yahoo.com
*nix forums Guru Wannabe


Joined: 15 Feb 2006
Posts: 103

PostPosted: Sun May 28, 2006 8:34 pm    Post subject: Re: x=(x=5,11) Reply with quote

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

Richard Tobin wrote:
In article <1148734636.495897.218270@j33g2000cwa.googlegroups.com>,
Robert Gamble <rgamble99@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.
Back to top
ena8t8si@yahoo.com
*nix forums Guru Wannabe


Joined: 15 Feb 2006
Posts: 103

PostPosted: Sun May 28, 2006 8:35 pm    Post subject: Re: x=(x=5,11) Reply with quote

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

Jordan Abel wrote:
On 2006-05-27, pemo <usenetmeister@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.
Back to top
ena8t8si@yahoo.com
*nix forums Guru Wannabe


Joined: 15 Feb 2006
Posts: 103

PostPosted: Sun May 28, 2006 8:42 pm    Post subject: Re: x=(x=5,11) Reply with quote

Tim Woodall wrote:
Quote:
On 27 May 2006 13:49:50 -0700,
ena8t8si@yahoo.com <ena8t8si@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.
Back to top
Tim Woodall
*nix forums beginner


Joined: 16 Jul 2005
Posts: 13

PostPosted: Sun May 28, 2006 8:55 pm    Post subject: Re: x=(x=5,11) Reply with quote

On 28 May 2006 13:34:28 -0700,
ena8t8si@yahoo.com <ena8t8si@yahoo.com> wrote:
Quote:

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

Richard Tobin wrote:
In article <1148734636.495897.218270@j33g2000cwa.googlegroups.com>,
Robert Gamble <rgamble99@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/
Back to top
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Sun May 28, 2006 9:27 pm    Post subject: Re: x=(x=5,11) Reply with quote

ena8t8si@yahoo.com said:

Quote:
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 en8t8si@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)
Back to top
Jordan Abel
*nix forums Guru


Joined: 25 Oct 2005
Posts: 1366

PostPosted: Sun May 28, 2006 9:48 pm    Post subject: Re: x=(x=5,11) Reply with quote

2006-05-28 <<1148848550.483533.158520@j55g2000cwa.googlegroups.com>>, ena8t8si@yahoo.com wrote:
Quote:

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

Jordan Abel wrote:
On 2006-05-27, pemo <usenetmeister@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.
Back to top
Jordan Abel
*nix forums Guru


Joined: 25 Oct 2005
Posts: 1366

PostPosted: Sun May 28, 2006 9:55 pm    Post subject: Re: x=(x=5,11) Reply with quote

2006-05-28 <<1148827138.626437.161980@i40g2000cwc.googlegroups.com>>, Harald van D?k wrote:
Quote:
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.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 3 of 8 [112 Posts] Goto page:  Previous  1, 2, 3, 4, 5, 6, 7, 8 Next
View previous topic :: View next topic
The time now is Fri Jan 09, 2009 5:00 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  


Debt Management | WoW Gold | Credit Cards UK | Loans | McDonalds
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2071s ][ Queries: 11 (0.0680s) ][ GZIP on - Debug on ]