Bug 5971 - Associativity of C operators
Summary: Associativity of C operators
Status: RESOLVED FIXED
Alias: None
Product: frysk
Classification: Unclassified
Component: general (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Unassigned
URL:
Keywords:
Depends on:
Blocks: 2246
  Show dependency treegraph
 
Reported: 2008-03-19 16:11 UTC by Teresa Thomas
Modified: 2008-03-19 19:17 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Teresa Thomas 2008-03-19 16:11:04 UTC
Incorrect evaluation for C operators that associate right-to-left.

Frysk:
(fhpd) print i
1
(fhpd) print i++ + ++i + i 
7

But on a GCC compiler:
int i=1;
printf ("%d\n", i++ + ++i + i);

Output: 6


Gdb also seems to have this bug:
(gdb) print i
$2 = 1
(gdb)  print i++ + ++i + i
$3 = 7
Comment 1 Petr Machata 2008-03-19 17:03:26 UTC
I believe this hits undefined behavior, because you change "i" twice between two
sequence points.  I would notabug this.
Comment 2 Teresa Thomas 2008-03-19 17:30:20 UTC
Even if I do something like: 

(fhpd) print i = 1
1
(fhpd) print i++ + i
3

frysk and gdb gives result 3, whereas C evaluates it to a 2, which is why I
think its a bug with associativity.
Comment 3 Petr Machata 2008-03-19 18:29:35 UTC
But that expression has the same problem.  You can't both change and read the
number in one expression (or, more preciselly, between two sequence points). 
You may want to read the exact wording of the C or C++ standard, but FWIW,
expressions like these always make me feel icky.
Comment 4 Teresa Thomas 2008-03-19 19:17:17 UTC
Closing bug. 

Would be a neat feature to get frysk to warn users of possibly undefined operations.