Weird result for modulus operation
Hi all, Firstly, I want to thank gcc developers for the wonderful compiler suite. I ran into a problem. I am not whether this is regression or not. I compiled the following code using gcc-4.2.3 ubuntu 8.04 x86-64 and gcc-4.1.2 fedora 8 i686 struct abc { int a; int b; }; void postfix(void) { struct abc abc; abc.b = 16; abc.a = 17; abc.a = abc.a++ % abc.b; printf("postfix a %d\n", abc.a); } The output that I get when postfix function is called is: postfix a 18 I expected the result to be something like this: postfix a 1 However when I compile the code using gcc-3.4 on fedora 8 i686. The result is : postfix a 1 which is the result I expected. So, is the result given by gcc version 4 compliant with the C standard or a bug? If it is compliant with the C standard, can somebody care to explain it to me why it behaves in such a way? Thank you in advance. Regards, Ang Way Chuang
Re: Weird result for modulus operation
Andrew Pinski wrote: On Tue, Apr 29, 2008 at 8:50 PM, Ang Way Chuang <[EMAIL PROTECTED]> wrote: abc.a = abc.a++ % abc.b; You are assigning to abc.a twice without a sequence point inbetween so this code is undefined as the order of evaluation of expressions without a sequence point is unspecified. Thanks for the speedy reply. But why this code: int a = 17, b = 16; a = a++ % 16; results in a = 2 then? I think I need to know what is sequence point. I'll google that. Thanks, Andrew Pinski
Re: Weird result for modulus operation
Andrew Pinski wrote: On Tue, Apr 29, 2008 at 9:08 PM, Ang Way Chuang <[EMAIL PROTECTED]> wrote: Thanks for the speedy reply. But why this code: int a = 17, b = 16; a = a++ % 16; results in a = 2 then? I think I need to know what is sequence point. I'll google that. As I mentioned, the code is undefined so it could be any value. Is there any flag in gcc that can provide warning to code that relies on undefined behaviours? Thanks, Andrew Pinski
Re: Weird result for modulus operation
Ang Way Chuang wrote: Andrew Pinski wrote: On Tue, Apr 29, 2008 at 9:08 PM, Ang Way Chuang <[EMAIL PROTECTED]> wrote: Thanks for the speedy reply. But why this code: int a = 17, b = 16; a = a++ % 16; results in a = 2 then? I think I need to know what is sequence point. I'll google that. As I mentioned, the code is undefined so it could be any value. Is there any flag in gcc that can provide warning to code that relies on undefined behaviours? Found it. -Wsequence-point which is enabled by -Wall. But gcc didn't fart out any warning with -Wall or -Wsequence-point flag :( Thanks, Andrew Pinski
Re: Weird result for modulus operation
Paolo Bonzini wrote: Ang Way Chuang wrote: Ang Way Chuang wrote: Andrew Pinski wrote: On Tue, Apr 29, 2008 at 9:08 PM, Ang Way Chuang <[EMAIL PROTECTED]> wrote: Thanks for the speedy reply. But why this code: int a = 17, b = 16; a = a++ % 16; results in a = 2 then? I think I need to know what is sequence point. I'll google that. As I mentioned, the code is undefined so it could be any value. Is there any flag in gcc that can provide warning to code that relies on undefined behaviours? Found it. -Wsequence-point which is enabled by -Wall. But gcc didn't fart out any warning with -Wall or -Wsequence-point flag :( You found a bug, it does point out the problem with the second example here. Huh? Now you got me confused. Since it is an undefined behaviour, gcc is free to whatever it likes. Though the answer given by the first and second examples show inconsistency in gcc in handling the undefined behaviour. I can't forward to gmane.comp.gcc.devel newsgroup with my account. Paolo
Re: Weird result for modulus operation
Paolo Bonzini wrote: Thanks for the speedy reply. But why this code: int a = 17, b = 16; a = a++ % 16; Huh? Now you got me confused. Since it is an undefined behaviour, gcc is free to whatever it likes. Sure, but if you ask gcc to signal a warning, it is supposed to do so. :-) It is a bug that gcc with -Wsequence-point signals a warning for "a = a++ % 16" but not when you use abc.a. Oic, thank you for the clarification. I didn't notice that the warning works for a = a++ % 16 because I only tried the -Wsequence-point flag on abc.a example. Though the answer given by the first and second examples show inconsistency in gcc in handling the undefined behaviour. That's not a problem. GCC does not have to be consistent. But both should be warned about. I can't forward to gmane.comp.gcc.devel newsgroup with my account. No problem, you can delete it. Paolo