public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/53382] New: incorrect associativity in expressions
@ 2012-05-16 23:06 miguel.barao at gmail dot com
  2012-05-16 23:08 ` [Bug c/53382] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: miguel.barao at gmail dot com @ 2012-05-16 23:06 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53382

             Bug #: 53382
           Summary: incorrect associativity in expressions
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: miguel.barao@gmail.com


I found the following behavior while writing a small RPN calculator.
The ideia is to perform a subtraction on the two top elements of a stack using

push(&mystack, -pop(&mystack) + pop(&mystack));

Since the + associativity is left-to-right, this should pop the stack, negate
this value, pop the second argument then add. But the order is reversed in gcc
leading to a wrong result.

The behavior was observed in gcc 4.4.5 (debian stable), 4.6.3 (ubuntu 12.04),
and the llvm-gcc versions shipping with MacOSX.
The clang frontend versions 1.1 (on debian stable), 3.0 (OSX and ubuntu 12.04)
work correctly.

Example:
Suppose a stack contains [ 1 2 ], the top of the stack being 2.
On gcc the above expression returns 1, the first pop executed is the second
one.
On clang the same expression returns -1, the first pop executed is the first
one as should be.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/53382] incorrect associativity in expressions
  2012-05-16 23:06 [Bug c/53382] New: incorrect associativity in expressions miguel.barao at gmail dot com
@ 2012-05-16 23:08 ` pinskia at gcc dot gnu.org
  2012-05-16 23:12 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-05-16 23:08 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53382

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-05-16 23:05:45 UTC ---
 -pop(&mystack) + pop(&mystack)

Either pop can come first as there is no sequence point between them.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/53382] incorrect associativity in expressions
  2012-05-16 23:06 [Bug c/53382] New: incorrect associativity in expressions miguel.barao at gmail dot com
  2012-05-16 23:08 ` [Bug c/53382] " pinskia at gcc dot gnu.org
@ 2012-05-16 23:12 ` pinskia at gcc dot gnu.org
  2012-05-16 23:15 ` pinskia at gcc dot gnu.org
  2012-05-17 10:19 ` miguel.barao at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-05-16 23:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53382

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-05-16 23:07:46 UTC ---
See also http://c-faq.com/expr/confused.html .


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/53382] incorrect associativity in expressions
  2012-05-16 23:06 [Bug c/53382] New: incorrect associativity in expressions miguel.barao at gmail dot com
  2012-05-16 23:08 ` [Bug c/53382] " pinskia at gcc dot gnu.org
  2012-05-16 23:12 ` pinskia at gcc dot gnu.org
@ 2012-05-16 23:15 ` pinskia at gcc dot gnu.org
  2012-05-17 10:19 ` miguel.barao at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-05-16 23:15 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53382

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-05-16 23:12:02 UTC ---
Basically C does not specify which order of the two operands of + are evaluated
 first so both clang and GCC are correct.

>Since the + associativity is left-to-right
Kinda but the order of evaluation is different from associativity.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/53382] incorrect associativity in expressions
  2012-05-16 23:06 [Bug c/53382] New: incorrect associativity in expressions miguel.barao at gmail dot com
                   ` (2 preceding siblings ...)
  2012-05-16 23:15 ` pinskia at gcc dot gnu.org
@ 2012-05-17 10:19 ` miguel.barao at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: miguel.barao at gmail dot com @ 2012-05-17 10:19 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53382

--- Comment #4 from Miguel Barao <miguel.barao at gmail dot com> 2012-05-17 09:42:48 UTC ---
There was a misunderstanding between associativity and operator evaluation
order in the original bugreport.
I now understand that the language does not specify the evaluation order of an
operator, and is bad a practice to depend such behavior.
Still, I would like to add that expressions like
- f( ) + f( )
and
- ( f( ) - f( ) ) 
produce different evaluation orders, and therefor different results, when f( )
has side effects.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-05-17  9:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-16 23:06 [Bug c/53382] New: incorrect associativity in expressions miguel.barao at gmail dot com
2012-05-16 23:08 ` [Bug c/53382] " pinskia at gcc dot gnu.org
2012-05-16 23:12 ` pinskia at gcc dot gnu.org
2012-05-16 23:15 ` pinskia at gcc dot gnu.org
2012-05-17 10:19 ` miguel.barao at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).