News for hackers. Stuff that matters.
Re:A very old gcc "bug" by Dilpil on Sunday October 25, 2009 @07:25PM (#902321)
My favorite gcc trick is to compile and run the following two line, and then try to reconcile the results.

x = 7;

printf("%d %d %d",x++,++x,x++);

x = 7;

printf("%d %d %d",x++,x=2,x++);

Go ahead, try it, run it, and explain the output.

Re:A very old gcc "bug" by vinutheraj on Sunday October 25, 2009 @08:14PM (#902380)
http://stackoverflow.com/questions/376278/parameter-evaluati...

https://www.securecoding.cert.org/confluence/display/seccode...

Basically, do not depend on the order of evaluation of arguments to a function call, because it is unspecified and left to the implementation to decide according to the C99 standard.

EDIT: Another explanation, pertinent to your example can be found here - http://c-faq.com/expr/evalorder2.html

Re:A very old gcc "bug" by ecaron on Sunday October 25, 2009 @08:15PM (#902381)
For those curious, the output is: 9 10 7 2 3 7

The output should be (at least from my understanding and what PHP renders): 7 9 9 7 2 2

As far as reconciliation, I guess the first line makes sense when you think about it as machine code and how the pointers probably behave. The second line though, seems quite evil and I am going to go hide under my "Rails will save me" blanket until someone can explain this voodoo.

Re:A very old gcc "bug" by yuvi on Sunday October 25, 2009 @08:30PM (#902405)
clang gives 7 9 9 7 2 2 as well. gcc gives 7 10 9 7 3 2 when targeting arm.

2,3,7 are all valid possibilities for the 4th and 6th numbers, but apparently gcc interprets the 5th as 2 + 1 somehow, which I can't see as being valid whatsoever.

Re:A very old gcc "bug" by ars on Sunday October 25, 2009 @08:44PM (#902428)
Why not? It's doing the x=2 first (before anything else in there), then the ++ of the last expression (and returning 2 for it), then giving you the result of the middle one (which is now three since it ran after the last one). The 7 is because it ran that one before doing anything to the other values.

It should be noted that this is illegal C, and compilers are allowed to do absolutely anything they want when they see it. Even things that are totally not logical.

Re:A very old gcc "bug" by yuvi on Sunday October 25, 2009 @09:06PM (#902452)
I re-read C99, and the reason this is allowed is not because of §6.5.2.2 p10, but rather §6.5.16 p4 as far as I can tell. Though it's weird to me that the expression (x = 2) can ever be evaluated as anything but 2.

Also, it's not illegal C, it's undefined C and compilers are only allowed to do whatever they want within the undefined area (which is larger than I expected.)

Re:A very old gcc "bug" by ars on Monday October 26, 2009 @12:00AM (#902611)
Undefined C is illegal C, because the compiler is not even required to do the same thing from run to run.

Undefined is not the same as implementation defined.

Re:A very old gcc "bug" by JoeAltmaier on Monday October 26, 2009 @10:13AM (#903212)
Normally C compilers push arguments right-to-left, so probably evaluate them that way too. Still can't explain x=2 coming out 3 tho...
Re:A very old gcc "bug" by burke on Sunday October 25, 2009 @08:36PM (#902411)
Very neat.

        movl    $7, -4(%rbp)
        movl    -4(%rbp), %ecx
        addl    $1, -4(%rbp)
        movl    $2, -4(%rbp)
        movl    -4(%rbp), %esi
        addl    $1, -4(%rbp)
        movl    -4(%rbp), %edx
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf