[[ This message was both posted and mailed: see
the "To," "Cc," and "Newsgroups" headers for details. ]]
The following snippet results in an infinite loop (as tested in CW 8.3
and CW 9.3, Mac). Specifically, a is not incremented. Can this be
right?
#include <stdio.h>
int main(void)
{
int a = 0;
int b = 10;
while (a < b)
{
a = a++;
}
return 0;
}
Season's Greetings to all!
Tony Patrinos
www.escape.gr
Eric VERGNAUD - 22 Dec 2004 12:01 GMT
dans l'article 221220041128194651%patrinos@escape.gr, Anthony Patrinos à
patrinos@escape.gr a écrit le 22/12/04 10:28 :
> [[ This message was both posted and mailed: see
> the "To," "Cc," and "Newsgroups" headers for details. ]]
[quoted text clipped - 22 lines]
> Tony Patrinos
> www.escape.gr
This is not a bug. The value copied to a is the value of a BEFORE a is
incremented, and it is copied AFTER a is incremented. So the code you wrote
reads as:
while(a<b)
{
int x = a;
a++;
a = x;
}
Maybe you wanted to write:
while(a<b)
{
a = ++a;
}
Eric
Anthony Patrinos - 22 Dec 2004 12:57 GMT
> dans l'article 221220041128194651%patrinos@escape.gr, Anthony Patrinos à
> patrinos@escape.gr a écrit le 22/12/04 10:28 :
[quoted text clipped - 45 lines]
>
> Eric
Thnx Eric for your reply...
The assembly dump agrees with your interpretation of the correct order
of evaluation.
However, I wonder if the following order of evaluation is inconsistent
with the language definition:
1. retrieve the old value of "a",
2. assign the old value of "a" to "a",
3. increment "a"
At least this must be the way this works on some systems because I've
encountered this situation while working on a port of the Panda PDF
library (linux, windows) to MacOS X with CW. The code has been working
for a number of years on the other systems.
Researching further, I found a similar situation (a[i] = i++;)
mentioned in K&R 2nd edition, p.53 as implementation dependent.
Perhaps the compiler should issue a warning on situations susch as the
above.
Regards,
Tony Patrinos
C.R. Osterwald - 22 Dec 2004 14:51 GMT
> > dans l'article 221220041128194651%patrinos@escape.gr, Anthony Patrinos à
> > patrinos@escape.gr a écrit le 22/12/04 10:28 :
[quoted text clipped - 68 lines]
> Perhaps the compiler should issue a warning on situations susch as the
> above.
Why is the assignment needed? Note that your original loop can be
written:
while (a < b)
{ ++a; }
--
Anthony Patrinos - 22 Dec 2004 16:12 GMT
I>
> Why is the assignment needed? Note that your original loop can be
> written:
[quoted text clipped - 3 lines]
>
> --
Well, it's not acually. But, as I mentioned, I came upon such a
construct in the open source Panda PDF library. I compiled it for OSX,
ran the sample program. It got stuck and debugging led to this
"discovery"...
-Tony Patrinos
Anthony Patrinos - 22 Dec 2004 13:12 GMT
> dans l'article 221220041128194651%patrinos@escape.gr, Anthony Patrinos à
> patrinos@escape.gr a écrit le 22/12/04 10:28 :
[quoted text clipped - 45 lines]
>
> Eric
Thnx Eric for your reply...
The assembly dump agrees with your interpretation of the correct order
of evaluation.
However, I wonder if the following order of evaluation is inconsistent
with the language definition:
1. retrieve the old value of "a",
2. assign the old value of "a" to "a",
3. increment "a"
At least this must be the way this works on some systems because I've
encountered this situation while working on a port of the Panda PDF
library (linux, windows) to MacOS X with CW. The code has been working
for a number of years on the other systems.
Researching further, I found a similar situation (a[i] = i++;)
mentioned in K&R 2nd edition, p.53 as implementation dependent.
Regards,
Tony Patrinos
Anthony Patrinos - 22 Dec 2004 16:20 GMT
> [[ This message was both posted and mailed: see
> the "To," "Cc," and "Newsgroups" headers for details. ]]
[quoted text clipped - 22 lines]
> Tony Patrinos
> www.escape.gr
I thought I'd test it with xCode (gcc back-end). The behavior is the
opposite of CW and there is not infinite loop.
-Tony Patrinos
Dave Baum - 22 Dec 2004 16:46 GMT
> [[ This message was both posted and mailed: see
> the "To," "Cc," and "Newsgroups" headers for details. ]]
[quoted text clipped - 22 lines]
> Tony Patrinos
> www.escape.gr
The statement "a=a++;" has undefined behavior. C makes very few
promises about the order of evaluation for expressions. Any expression
that either modifies the same object twice, or modifies it and uses it
elsewhere in the expression is undefined. So the following are all very
bad...
a = a++;
x[a] = a++;
y = a++ + a;
The comp.lang.C FAQ (http://www.eskimo.com/~scs/C-faq/top.html) has some
more detailed information about this (specifically questions 3.1, 3.2,
and 3.9).
Dave
Gianluca Silvestri - 22 Dec 2004 17:15 GMT
> [[ This message was both posted and mailed: see
> the "To," "Cc," and "Newsgroups" headers for details. ]]
[quoted text clipped - 22 lines]
> Tony Patrinos
> www.escape.gr
I think this is undefined behavior according to the Standard, beacause the
value is written more than once between two sequence points.
Gianluca