This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/47412] Output: x = 31 but y = 30
- From: "hamotahari at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 22 Jan 2011 15:51:23 +0000
- Subject: [Bug c++/47412] Output: x = 31 but y = 30
- Auto-submitted: auto-generated
- References: <bug-47412-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47412
HM <hamotahari at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |hamotahari at gmail dot com
--- Comment #2 from HM <hamotahari at gmail dot com> 2011-01-22 15:51:21 UTC ---
(In reply to comment #1)
> (In reply to comment #0)
> > x = 20 + x++;
>
> This is undefined behaviour because there is no sequence point between the
> increment and the assignment.
>
> Compiling with -Wall warns you about it
I know the word "the behavior is undefined", and then this can not be a bug.
But I wrote this code in Java as the below:
public class TestXPP {
/**
* @param args
*/
public static void main(String[] args) {
int x = 10;
x = 20 + x++;
System.out.println("X is: " + x);
x = 10;
int y = 20 + x++;
System.out.println("Y is: " + y);
}
}
And the output, both are 30:
X is: 30
Y is: 30
It seems interesting for me. It means Java doesn't assume it as an undefined
behavior point or at least Java's compiler does in a unique way and doesn't
care about being X or Y.
Can anyone explain it?