First Last Prev Next    No search results available      Search page      Enter new bug
Bug#: 11751
Product:  
Component:  
Status: RESOLVED
Resolution: INVALID
Assigned To: Not yet assigned to anyone <unassigned@gcc.gnu.org>
Host:
Reported against  
Priority:  
Severity:  
Target Milestone:  
 
 
Target:
Reporter: Elitsa Mladenova <me@elitsa.net>
Add CC:
CC:
Remove selected CCs
Build:
URL:
Summary:
Keywords:
Known to work:
Known to fail:

Attachment Description Type Created Size Actions
Create a New Attachment (proposed patch, testcase, etc.) View All

Bug 11751 depends on: Show dependency tree
Show dependency graph
Bug 11751 blocks:

Additional Comments:






View Bug Activity   |   Format For Printing   |   Clone This Bug


Description:   Last confirmed: Opened: 2003-07-31 17:52
bellow is simple code that express this bug:

/************
 * File : b.C
 ************/

#include <stdio.h>

int main()
        {
        int a = 1;
        printf(" a = %i \n",(a++)+(++a));               //4

        a = 1;
        printf(" a = %i \n",(a++)+(a++)+(++a));         //4

        return 0;
        }






After compilation in a following way:

c++ -o b b.C

The result was:

 a = 4 
 a = 4

------- Comment #1 From Andrew Pinski 2003-07-31 17:55 -------
Not a bug in gcc but rather in your code.  Read about sequence points and
modifying variables 
between them.

------- Comment #2 From Wolfgang Bangerth 2004-08-05 15:01 -------
*** Bug 15012 has been marked as a duplicate of this bug. ***

------- Comment #3 From Wolfgang Bangerth 2004-08-05 15:01 -------
*** Bug 15103 has been marked as a duplicate of this bug. ***

------- Comment #4 From Wolfgang Bangerth 2004-08-05 15:01 -------
*** Bug 14417 has been marked as a duplicate of this bug. ***

------- Comment #5 From Wolfgang Bangerth 2004-08-05 15:01 -------
*** Bug 13403 has been marked as a duplicate of this bug. ***

------- Comment #6 From Wolfgang Bangerth 2004-08-05 15:02 -------
*** Bug 12552 has been marked as a duplicate of this bug. ***

------- Comment #7 From Wolfgang Bangerth 2004-08-13 15:39 -------
*** Bug 11363 has been marked as a duplicate of this bug. ***

------- Comment #8 From Wolfgang Bangerth 2004-08-13 15:40 -------
*** Bug 17018 has been marked as a duplicate of this bug. ***

------- Comment #9 From Wolfgang Bangerth 2004-08-13 15:45 -------
*** Bug 14951 has been marked as a duplicate of this bug. ***

------- Comment #10 From Wolfgang Bangerth 2004-08-13 15:46 -------
*** Bug 16887 has been marked as a duplicate of this bug. ***

------- Comment #11 From Wolfgang Bangerth 2004-08-13 16:03 -------
*** Bug 816 has been marked as a duplicate of this bug. ***

------- Comment #12 From Wolfgang Bangerth 2004-08-13 16:03 -------
*** Bug 4978 has been marked as a duplicate of this bug. ***

------- Comment #13 From Wolfgang Bangerth 2004-08-13 16:03 -------
*** Bug 5724 has been marked as a duplicate of this bug. ***

------- Comment #14 From Wolfgang Bangerth 2004-08-13 16:03 -------
*** Bug 6790 has been marked as a duplicate of this bug. ***

------- Comment #15 From Wolfgang Bangerth 2004-08-13 16:04 -------
*** Bug 7135 has been marked as a duplicate of this bug. ***

------- Comment #16 From Wolfgang Bangerth 2004-08-13 16:04 -------
*** Bug 9693 has been marked as a duplicate of this bug. ***

------- Comment #17 From Wolfgang Bangerth 2004-08-13 16:04 -------
*** Bug 16309 has been marked as a duplicate of this bug. ***

------- Comment #18 From Wolfgang Bangerth 2004-08-13 16:05 -------
*** Bug 10947 has been marked as a duplicate of this bug. ***

------- Comment #19 From Volker Reichelt 2004-08-19 08:01 -------
*** Bug 17095 has been marked as a duplicate of this bug. ***

------- Comment #20 From Wolfgang Bangerth 2004-08-31 20:46 -------
*** Bug 17253 has been marked as a duplicate of this bug. ***

------- Comment #21 From Andreas Schwab 2004-09-02 12:36 -------
*** Bug 17282 has been marked as a duplicate of this bug. ***

------- Comment #22 From Giovanni Bajo 2004-09-02 13:00 -------
Volker recently added a small paragraph about this very common non-bug to our 
non-bug page:

http://gcc.gnu.org/bugs.html#nonbugs_c

You can read here about this bug.

------- Comment #23 From Andrew Pinski 2004-09-03 05:23 -------
*** Bug 17282 has been marked as a duplicate of this bug. ***

------- Comment #24 From Volker Reichelt 2004-09-15 19:51 -------
*** Bug 17507 has been marked as a duplicate of this bug. ***

------- Comment #25 From Andrew Pinski 2004-09-23 20:54 -------
*** Bug 17639 has been marked as a duplicate of this bug. ***

------- Comment #26 From Paolo Carlini 2005-02-07 00:19 -------
*** Bug 19798 has been marked as a duplicate of this bug. ***

------- Comment #27 From Andrew Pinski 2005-02-23 20:26 -------
*** Bug 20181 has been marked as a duplicate of this bug. ***

------- Comment #28 From Dion Picco 2005-02-23 20:38 -------
The point I was making with my example is that the native types (int, long,
char, etc...) have different behaviour than a user-defined class with the
operator++.  If it is compiler dependent which way the expression is evaluated,
why not at least make them both agree?  GCC is also the only compiler out of the
5 that I've tested that exhibits this behaviour... all others unify the
behaviour of native and user-defined operator++. 

------- Comment #29 From Andrew Pinski 2005-02-23 20:41 -------
(In reply to comment #28)
The code is undefined, which means we should be able to do system("rm -Rf /");, note we don't.

------- Comment #30 From Dion Picco 2005-02-23 20:46 -------
Here is a better clarification:

Case 1
======
int a = 0;
int b = a++ + a++;
printf("b = %d\n", b);  // output is 0


Case 2
======
class A
{
  int a_;
public:
  A() : a_(0) {}

  int operator++() { return a_++; }
};

A a;
int b = a++ + a++;
printf("b = %d\n", b);  // output is 1

This is a simple case that shows how the behaviour of the operator++ should be
united.

I'm not sure what you mean by the system(...) call... I understand that the code
is undefined (meaning its up to the compiler vendor to implement as they see
fit).  I think the most fitting way is to have the above two cases unified in
behaviour... isn't one of the reasons that operators were added to C++ was to
allow user-defined types to mimic the functionality and usability of the native
C types?

------- Comment #31 From Andrew Pinski 2005-02-23 20:51 -------
(In reply to comment #30)
> I'm not sure what you mean by the system(...) call... I understand that the code
> is undefined (meaning its up to the compiler vendor to implement as they see
> fit).  I think the most fitting way is to have the above two cases unified in
> behaviour... isn't one of the reasons that operators were added to C++ was to
> allow user-defined types to mimic the functionality and usability of the native
> C types?

Undefined means that we can do anything.  Which where the system call comes from.  The point is that 
this undefined, there does not even have be a constancy in the behavior across optimization levels, 
types or anything else.


------- Comment #32 From Dion Picco 2005-02-23 23:27 -------
I won't press the issue further because I have other things more pressing ;)

But I think the decision to not change the behaviour here is wrong.  I cannot
create an Integer class that acts as an int due to the operator++.  Just because
it is undefined does not mean that we have to arbitrarily have to choose a
method that adds no value over one that adds good value.  Nearly all other
compiler vendors have adopted this method since it has an element of
common-sense in the face of an undefined process.  I get the feeling that the
only reason it isn't changed is simply because no-one wants to do it.  If there
is another reason, what is it?  

------- Comment #33 From Paul Schlie 2005-02-24 10:34 -------
Although I can confidently say that I've been less than enthusiastic with
some of GCC's standards interpretations; here GCC's results in each of the
examples you cite are within the set of semantically consent values which
should be expected to result from an unspecified evaluation and/or value
assignment order. (Although do agree that GCC has no license to return any
value other than those which would result from these ordering ambiguities).

(Although do believe that GCC should adopt an lr evaluation order; as
expressions which are otherwise ambitious are useless, and those which are
unaffected are insensitive to it; resulting in no harm, only benefit; and with a
little luck may lead to the C/C++ committees coming to their senses in time.)

------- Comment #34 From Andrew Pinski 2005-04-20 02:42 -------
*** Bug 3165 has been marked as a duplicate of this bug. ***

------- Comment #35 From Andrew Pinski 2005-04-20 02:47 -------
*** Bug 1039 has been marked as a duplicate of this bug. ***

------- Comment #36 From Andrew Pinski 2005-04-20 02:54 -------
*** Bug 2550 has been marked as a duplicate of this bug. ***

------- Comment #37 From Andrew Pinski 2005-04-20 02:55 -------
*** Bug 3324 has been marked as a duplicate of this bug. ***

------- Comment #38 From Andrew Pinski 2005-04-20 02:56 -------
*** Bug 5159 has been marked as a duplicate of this bug. ***

------- Comment #39 From Andrew Pinski 2005-04-20 02:57 -------
*** Bug 5494 has been marked as a duplicate of this bug. ***

------- Comment #40 From Andrew Pinski 2005-04-20 02:58 -------
*** Bug 5516 has been marked as a duplicate of this bug. ***

------- Comment #41 From Andrew Pinski 2005-04-20 02:59 -------
*** Bug 6409 has been marked as a duplicate of this bug. ***

------- Comment #42 From Andrew Pinski 2005-04-20 03:03 -------
*** Bug 9334 has been marked as a duplicate of this bug. ***

------- Comment #43 From Andrew Pinski 2005-04-20 03:05 -------
*** Bug 9675 has been marked as a duplicate of this bug. ***

------- Comment #44 From Andrew Pinski 2005-04-21 04:58 -------
*** Bug 6409 has been marked as a duplicate of this bug. ***

------- Comment #45 From Giovanni Bajo 2005-04-27 11:40 -------
*** Bug 21246 has been marked as a duplicate of this bug. ***

------- Comment #46 From Andrew Pinski 2005-05-05 18:12 -------
*** Bug 21404 has been marked as a duplicate of this bug. ***

------- Comment #47 From Andrew Pinski 2005-06-30 13:26 -------
*** Bug 22248 has been marked as a duplicate of this bug. ***

------- Comment #48 From Andrew Pinski 2005-06-30 13:37 -------
*** Bug 22248 has been marked as a duplicate of this bug. ***

------- Comment #49 From Andrew Pinski 2005-07-01 14:29 -------
*** Bug 22248 has been marked as a duplicate of this bug. ***

------- Comment #50 From Andrew Pinski 2005-09-22 17:07 -------
*** Bug 24015 has been marked as a duplicate of this bug. ***

------- Comment #51 From Andrew Pinski 2005-11-10 03:17 -------
*** Bug 1570 has been marked as a duplicate of this bug. ***

------- Comment #52 From Andrew Pinski 2005-11-10 03:17 -------
*** Bug 762 has been marked as a duplicate of this bug. ***

------- Comment #53 From Andrew Pinski 2005-11-10 03:18 -------
*** Bug 5051 has been marked as a duplicate of this bug. ***

------- Comment #54 From Andrew Pinski 2005-11-10 03:48 -------
*** Bug 8175 has been marked as a duplicate of this bug. ***

------- Comment #55 From Andrew Pinski 2005-11-10 03:49 -------
*** Bug 6765 has been marked as a duplicate of this bug. ***

------- Comment #56 From Andrew Pinski 2005-11-10 03:49 -------
*** Bug 6891 has been marked as a duplicate of this bug. ***

------- Comment #57 From Andrew Pinski 2005-12-12 20:23 -------
*** Bug 2673 has been marked as a duplicate of this bug. ***

------- Comment #58 From Andreas Schwab 2006-02-01 10:34 -------
*** Bug 26060 has been marked as a duplicate of this bug. ***

------- Comment #59 From Andrew Pinski 2006-02-22 14:04 -------
*** Bug 26418 has been marked as a duplicate of this bug. ***

------- Comment #60 From Andrew Pinski 2006-03-11 15:09 -------
*** Bug 26642 has been marked as a duplicate of this bug. ***

------- Comment #61 From Emil Obermayr 2006-03-11 16:10 -------
referring to duplicate 26642: 

The behavior changed between gcc3 and gcc4 and the comment is "there is no
reason the result should not change"?

Sorry, but I think that's a really bad way to handle things. When a update
changes behavior, there should be et least an appropriate warning.

And another comment says "look at the warning you got". If you'd read the
warnings you would have seen it refers to that part of the code that didn't
change its behavior. So a code that only uses the array-version (that behaves
differently) wouldn't get the warning.

I really doubt that there is any good reason that simple variables and array
variables should differ in the order their expressions are solved.

Please, think about it. At least give a warning at the right place!

------- Comment #62 From Andrew Pinski 2006-03-11 16:14 -------
(In reply to comment #61)
> referring to duplicate 26642: 
> 
> The behavior changed between gcc3 and gcc4 and the comment is "there is no
> reason the result should not change"?

Why this is undefined code :).

------- Comment #63 From Daniel Berlin 2006-03-11 16:49 -------
(In reply to comment #61)
> referring to duplicate 26642: 
> 
> The behavior changed between gcc3 and gcc4 and the comment is "there is no
> reason the result should not change"?

There is simply no guarantee at all that the behavior of this will or won't
change.
It's undefined code.  It could change depending on the phase of the moon.
> 
> Sorry, but I think that's a really bad way to handle things. When a update
> changes behavior, there should be et least an appropriate warning.

Unfortunately, it is impossible to warn for every permuation of undefined code
at compilation time.
However, in general, if you write code whose semantics are undefined, you are
going to get burned.  This is just the way the ball bounces.

------- Comment #64 From Andrew Pinski 2006-03-17 16:59 -------
*** Bug 26730 has been marked as a duplicate of this bug. ***

------- Comment #65 From Andrew Pinski 2006-03-23 06:30 -------
*** Bug 26820 has been marked as a duplicate of this bug. ***

------- Comment #66 From Andrew Pinski 2006-03-29 19:26 -------
*** Bug 26923 has been marked as a duplicate of this bug. ***

------- Comment #67 From Andrew Pinski 2006-04-14 02:31 -------
*** Bug 27153 has been marked as a duplicate of this bug. ***

------- Comment #68 From Andrew Pinski 2006-04-14 02:50 -------
*** Bug 27153 has been marked as a duplicate of this bug. ***

------- Comment #69 From Andrew Pinski 2006-04-14 16:54 -------
*** Bug 27153 has been marked as a duplicate of this bug. ***

------- Comment #70 From Andrew Pinski 2006-04-21 00:40 -------
*** Bug 27233 has been marked as a duplicate of this bug. ***

------- Comment #71 From Andrew Pinski 2006-04-28 00:00 -------
*** Bug 27344 has been marked as a duplicate of this bug. ***

------- Comment #72 From Andrew Pinski 2006-05-17 19:06 -------
*** Bug 27646 has been marked as a duplicate of this bug. ***

------- Comment #73 From Richard Guenther 2007-02-23 15:36 -------
*** Bug 30935 has been marked as a duplicate of this bug. ***

------- Comment #74 From Richard Guenther 2007-03-30 13:03 -------
*** Bug 31398 has been marked as a duplicate of this bug. ***

------- Comment #75 From Andrew Pinski 2007-05-24 16:21 -------
*** Bug 32067 has been marked as a duplicate of this bug. ***

------- Comment #76 From Andrew Pinski 2007-05-28 22:47 -------
*** Bug 32133 has been marked as a duplicate of this bug. ***

------- Comment #77 From Andreas Schwab 2007-06-28 16:54 -------
*** Bug 32536 has been marked as a duplicate of this bug. ***

------- Comment #78 From Gregor Jasny 2007-08-10 12:02 -------
*** Bug 33043 has been marked as a duplicate of this bug. ***

------- Comment #79 From Richard Guenther 2007-08-30 12:33 -------
*** Bug 33248 has been marked as a duplicate of this bug. ***

------- Comment #80 From Andreas Schwab 2007-08-30 16:10 -------
*** Bug 33248 has been marked as a duplicate of this bug. ***

------- Comment #81 From Andrew Pinski 2007-08-31 16:09 -------
*** Bug 33270 has been marked as a duplicate of this bug. ***

------- Comment #82 From Paolo Carlini 2008-10-10 16:33 -------
*** Bug 37800 has been marked as a duplicate of this bug. ***

------- Comment #83 From Andreas Schwab 2008-12-13 18:26 -------
*** Bug 38516 has been marked as a duplicate of this bug. ***

------- Comment #84 From Andrew Pinski 2009-02-09 18:45 -------
*** Bug 39143 has been marked as a duplicate of this bug. ***

------- Comment #85 From Andreas Schwab 2009-06-07 09:04 -------
*** Bug 40366 has been marked as a duplicate of this bug. ***

First Last Prev Next    No search results available      Search page      Enter new bug