public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/51562] New: Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4
@ 2011-12-15  7:10 willus0 at hotmail dot com
  2011-12-15 10:03 ` [Bug c/51562] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: willus0 at hotmail dot com @ 2011-12-15  7:10 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 51562
           Summary: Expression evaluation with commas seems incorrect in
                    gcc 4.5.2, 4.4.4
    Classification: Unclassified
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: willus0@hotmail.com


I apologize if this has been reported or isn't even a bug, but I wasn't even
sure what to search for.  The code below doesn't evaluate the way you think it
might in gcc 4.5.2 (and gcc 4.4.4).  I'm not even sure how it should
legally/correctly evaluate, but there is a powell() function in Numerical
Recipes in C (1st and 2nd editions) that uses code like below and expects the
answer to the second line to be 5 (i.e. for the x*x expression to be evaluated
with the two different values of x and summed correctly).  That's how I
discovered this issue.  gcc-compiled code in gcc 4.5.2/4.4.4 reports this:

1+2=4
1^2+2^2=8

I tried different levels of optimization, on both Linux and Windows (MinGW),
all with the same result.  Tiny CC-compiled code reports this:

1+2=4
1^2+2^2=5

Those are the only compilers I've tried.  Here's the program:

#include <stdio.h>
void main(void)
    {
    int x;
    printf("1+2=%d\n",(x=1,x)+(x=2,x));
    printf("1^2+2^2=%d\n",(x=1,x*x)+(x=2,x*x));
    }

Thoughts??


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

* [Bug c/51562] Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4
  2011-12-15  7:10 [Bug c/51562] New: Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4 willus0 at hotmail dot com
@ 2011-12-15 10:03 ` rguenth at gcc dot gnu.org
  2011-12-15 13:27 ` willus0 at hotmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-12-15 10:03 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

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

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-12-15 09:57:32 UTC ---
(x=1,x)+(x=2,x)

is invoking undefined behavior as there is no sequence point inbetween
the two assignments to x.


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

* [Bug c/51562] Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4
  2011-12-15  7:10 [Bug c/51562] New: Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4 willus0 at hotmail dot com
  2011-12-15 10:03 ` [Bug c/51562] " rguenth at gcc dot gnu.org
@ 2011-12-15 13:27 ` willus0 at hotmail dot com
  2011-12-15 15:19 ` [Bug c/51562] missing -Wsequence-point warning for expression with commas manu at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: willus0 at hotmail dot com @ 2011-12-15 13:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from willus0 at hotmail dot com 2011-12-15 13:16:37 UTC ---
(In reply to comment #1)
> (x=1,x)+(x=2,x)
> 
> is invoking undefined behavior as there is no sequence point inbetween
> the two assignments to x.

Is the same also true of the second statement, in which case the Numerical
Recipes code is simply poorly written?


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

* [Bug c/51562] missing -Wsequence-point warning for expression with commas.
  2011-12-15  7:10 [Bug c/51562] New: Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4 willus0 at hotmail dot com
  2011-12-15 10:03 ` [Bug c/51562] " rguenth at gcc dot gnu.org
  2011-12-15 13:27 ` willus0 at hotmail dot com
@ 2011-12-15 15:19 ` manu at gcc dot gnu.org
  2011-12-16 14:12 ` willus0 at hotmail dot com
  2015-03-16 15:32 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: manu at gcc dot gnu.org @ 2011-12-15 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |NEW
            Version|4.5.2                       |4.7.0
           Keywords|                            |diagnostic
   Last reconfirmed|                            |2011-12-15
                 CC|                            |manu at gcc dot gnu.org
         Resolution|INVALID                     |
     Ever Confirmed|0                           |1
            Summary|Expression evaluation with  |missing -Wsequence-point
                   |commas seems incorrect in   |warning for expression with
                   |gcc 4.5.2, 4.4.4            |commas.

--- Comment #3 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2011-12-15 14:45:46 UTC ---
Not sure why -Wsequence-point is missing this, but it is a bug.

Clang 3.1 does not warn either.


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

* [Bug c/51562] missing -Wsequence-point warning for expression with commas.
  2011-12-15  7:10 [Bug c/51562] New: Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4 willus0 at hotmail dot com
                   ` (2 preceding siblings ...)
  2011-12-15 15:19 ` [Bug c/51562] missing -Wsequence-point warning for expression with commas manu at gcc dot gnu.org
@ 2011-12-16 14:12 ` willus0 at hotmail dot com
  2015-03-16 15:32 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: willus0 at hotmail dot com @ 2011-12-16 14:12 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from willus0 at hotmail dot com 2011-12-16 14:01:31 UTC ---
Thanks Richard and Manuel for prompting me to learn more about sequence points.
 After 25 years of C programming, I've learned something new.  A couple of
links I found helpful:

http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html

http://en.wikipedia.org/wiki/Sequence_point


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

* [Bug c/51562] missing -Wsequence-point warning for expression with commas.
  2011-12-15  7:10 [Bug c/51562] New: Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4 willus0 at hotmail dot com
                   ` (3 preceding siblings ...)
  2011-12-16 14:12 ` willus0 at hotmail dot com
@ 2015-03-16 15:32 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-03-16 15:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51562

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |mpolacek at gcc dot gnu.org
         Resolution|---                         |DUPLICATE

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
PR65430 has a better testcase + explanation why that happens, so closing this
one as a dup.

*** This bug has been marked as a duplicate of bug 65430 ***


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

end of thread, other threads:[~2015-03-16 15:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-15  7:10 [Bug c/51562] New: Expression evaluation with commas seems incorrect in gcc 4.5.2, 4.4.4 willus0 at hotmail dot com
2011-12-15 10:03 ` [Bug c/51562] " rguenth at gcc dot gnu.org
2011-12-15 13:27 ` willus0 at hotmail dot com
2011-12-15 15:19 ` [Bug c/51562] missing -Wsequence-point warning for expression with commas manu at gcc dot gnu.org
2011-12-16 14:12 ` willus0 at hotmail dot com
2015-03-16 15:32 ` mpolacek at gcc dot gnu.org

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).