This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: Is this a bug?
Thank both of you, I didn't think the problem in right way -- the
behavior is undefined, I have been thinking it was caused by volatile
key word, your explanation makes me released. -Honggang
-----Original Message-----
From: Andrew Haley [mailto:aph@redhat.com]
Sent: Thursday, July 16, 2009 11:22 AM
To: David Daney
Cc: Honggang Xu; gcc-help@gcc.gnu.org
Subject: Re: Is this a bug?
David Daney wrote:
> Honggang Xu wrote:
>> Or my understand of keyword "volatile" is wrong, following code
>> outputs compiled by gcc 4.1.1: x=22 ,y=59
>>
>> main()
>> {
>> volatile int x=20,y=35;
>> x=y++ + x++;
>> y= ++y + ++x;
>> printf("x=%d y=%d\n" ,x,y);
>>
>> }
>>
>
> Your program has undefined behavior, the volatile may change the
> output, but it doesn't change the fact that its behavior is undefined.
>
> The problem is that the affect of the increment operator can take
> place either before or after the affect of the assignment. The
> compiler can order the affects any way that it desires between
sequence points.
Actually, that's not quite true: as this expression exhibits undefined
behaviour, the compiler can return absolutely anything: it isn't limited
to just where it does the increment.
Andrew.