473,903 Members | 4,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
May 27 '06
111 4952
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************ **********@j33g 2000cwa.googleg roups.com>,
>> Robert Gamble <rg*******@gmai l.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
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.c om'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
2006-05-28 <<11*********** ***********@j55 g2000cwa.google groups.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(fo o,"hello"),spri ntf(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

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(fo o,"hello"),spri ntf(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
2006-05-28 <<11*********** ***********@i40 g2000cwc.google groups.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
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 (notwithstandin g the as-if rule).

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

May 28 '06 #46
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 (notwithstandin g 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
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
2006-05-28 <11************ **********@i40g 2000cwc.googleg roups.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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10906
by: Scott Beavers | last post by:
I'm trying to create a form in Excel to sort from the form and take the data to another worksheet. I am very new to this and any help would be appreciated. I have a value in a cell that will direct what sheet I want the cells copied to, so I thought I could use an If statement but have been unable to right the code properly, ie... If "f2" = 1 Then Sheets("XX1").Select
0
3332
by: jason | last post by:
Hello - looking for an efficient way in perl (been a while) to use a control file to pass or fail strings submitted to a perl program. I have a flat file allow.txt in the following format. It does not have to be in this format, it can be in regex format - and maybe should be. f1 -+f2 -+f3 -+f4 -+f6 ... records in the file may look as follows (for example)
5
1551
by: Gianni Mariani | last post by:
The 3 compilers I tried all did different things. gcc 3.4.0 ICE's (Bug 15480) MS C++ 7.1 has meaningless diags Comeau accepts the code Obviously this is not somthing I'll be using until we get the compilers doing the right thing, but I do want to report the appropriate errors. BTW, I think the code is valid. It does the thing I was trying to do
16
5683
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
28
5963
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See section 4.7 for Rounding issues.
62
12288
jkmyoung
by: jkmyoung | last post by:
Does anyone have some super, super hard Sudoku puzzles? Back in February this year, I had enough time to finally program a Sudoku solver in Java. Right now, I'm looking for solvable puzzles, but ones that my program cannot solve. However, most hard puzzles in the newspapers are solved within 2-3 cycles so far. The ones I've found on google which are said to be hard, get solved in 3 cycles. Any help would be very much appreciated!...
7
2171
by: mandanarchi | last post by:
I found the answer last time I was 'playing' with xsl, but I didn't need to use it then and like an idiot, I didn't save where I found it. I've tried Google but it's not coming up with what I need. From Access I'm exporting a file to xml in the following format: <XMLTemp> <Who>JoeSmith</Who> <Contact>Billy Joe</Contact> <telephone>123457980</telephone> <PostCode>XX1 2XX</PostCode> <When>01/01/2009</When>
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4725
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.