public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* “x--” would self-add before opration "--"  ?
@ 2010-06-07  7:47 pem
  2010-06-07  9:33 ` Paolo Bonzini
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: pem @ 2010-06-07  7:47 UTC (permalink / raw)
  To: gcc

Hello list,

Yestoday I encounter this problem during a test:

1    int x = 11;
2    std::cout<< x <<  x-- << ++x;

I think it should be :
11 11 11

----
I wrote the fellowing code :

#include <iostream>

int main() {
    int x = 11;
    std::cout<< x <<  x-- << ++x << std::endl;
}

compile the code above with g++ 4.5.0, after run, it gives:
11 12 11

----
#include <stdio.h>

int main() {
   int x = 11;
   printf("%d %d %d\n", x, x--, ++x);
}

the c code compiled with gcc gives the same result.

----
But when I use clang and clang++ to compile the codes above, I get:
11 11 11. 

I am not familar with both c++ and compiler implementation, donot konw 
why the results are differnt for gcc and clang. Anyone could help and 
explain this difference for me?


Thanks,

Ryan Ren

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

* Re: “x--” would self-add before opration "--"  ?
  2010-06-07  7:47 “x--” would self-add before opration "--" ? pem
@ 2010-06-07  9:33 ` Paolo Bonzini
  2010-06-07 11:06 ` pem
  2010-06-07 11:40 ` “x--” would self-add before opration "-- " ? Andi Hellmund
  2 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2010-06-07  9:33 UTC (permalink / raw)
  To: pem; +Cc: gcc

On 06/07/2010 09:38 AM, pem wrote:
> I am not familar with both c++ and compiler implementation, donot konw
> why the results are differnt for gcc and clang. Anyone could help and
> explain this difference for me?

First of all, this would be a question for gcc-help@gcc.gnu.org.  This 
mailing list is for development _of_ gcc, not _with_ gcc.

Your question is answered by the documentation of -Wsequence-point:

`-Wsequence-point'
      Warn about code that may have undefined semantics because of
      violations of sequence point rules in the C and C++ standards.

      The C and C++ standards defines the order in which expressions in
      a C/C++ program are evaluated in terms of "sequence points", which
      represent a partial ordering between the execution of parts of the
      program: those executed before the sequence point, and those
      executed after it.  These occur after the evaluation of a full
      expression (one which is not part of a larger expression), after
      the evaluation of the first operand of a `&&', `||', `? :' or `,'
      (comma) operator, before a function is called (but after the
      evaluation of its arguments and the expression denoting the called
      function), and in certain other places.  Other than as expressed
      by the sequence point rules, the order of evaluation of
      subexpressions of an expression is not specified.  All these rules
      describe only a partial order rather than a total order, since,
      for example, if two functions are called within one expression
      with no sequence point between them, the order in which the
      functions are called is not specified.  However, the standards
      committee have ruled that function calls do not overlap.

      It is not specified when between sequence points modifications to
      the values of objects take effect.  Programs whose behavior
      depends on this have undefined behavior; the C and C++ standards
      specify that "Between the previous and next sequence point an
      object shall have its stored value modified at most once by the
      evaluation of an expression.  Furthermore, the prior value shall
      be read only to determine the value to be stored.".  If a program
      breaks these rules, the results on any particular implementation
      are entirely unpredictable.

      Examples of code with undefined behavior are `a = a++;', `a[n] =
      b[n++]' and `a[i++] = i;'.  Some more complicated cases are not
      diagnosed by this option, and it may give an occasional false
      positive result, but in general it has been found fairly effective
      at detecting this sort of problem in programs.

      The standard is worded confusingly, therefore there is some debate
      over the precise meaning of the sequence point rules in subtle
      cases.  Links to discussions of the problem, including proposed
      formal definitions, may be found on the GCC readings page, at
      `http://gcc.gnu.org/readings.html'.

      This warning is enabled by `-Wall' for C and C++.

In general, I suggest that you always use the -Wall option, so that you 
will get warnings for dobious code such as the one you pointed out.

Paolo

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

* Re: “x--” would self-add before opration "--"  ?
  2010-06-07  7:47 “x--” would self-add before opration "--" ? pem
  2010-06-07  9:33 ` Paolo Bonzini
@ 2010-06-07 11:06 ` pem
  2010-06-07 11:40 ` “x--” would self-add before opration "-- " ? Andi Hellmund
  2 siblings, 0 replies; 4+ messages in thread
From: pem @ 2010-06-07 11:06 UTC (permalink / raw)
  To: gcc


Sorry for missing version info of my compiler:

$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.5.0/lto-
wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr --enable-
languages=c,c++,fortran,objc,obj-c++,ada --enable-shared --enable-
threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-lto --
enable-gnu-unique-object --disable-multilib --disable-libstdcxx-pch --
with-system-zlib --with-ppl --with-cloog --libdir=/usr/lib --
libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
Thread model: posix
gcc version 4.5.0 (GCC) 

$ clang -v
clang version 1.1 (branches/release_27)
Target: x86_64-unknown-linux-gnu
Thread model: posix

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

* Re:  “x--” would self-add before opration "-- "  ?
  2010-06-07  7:47 “x--” would self-add before opration "--" ? pem
  2010-06-07  9:33 ` Paolo Bonzini
  2010-06-07 11:06 ` pem
@ 2010-06-07 11:40 ` Andi Hellmund
  2 siblings, 0 replies; 4+ messages in thread
From: Andi Hellmund @ 2010-06-07 11:40 UTC (permalink / raw)
  To: pem; +Cc: gcc

> I am not familar with both c++ and compiler implementation, donot konw
> why the results are differnt for gcc and clang. Anyone could help and
> explain this difference for me?

The ISO C standard says that the evaluation order of function arguments is
unspecified [ISO C99, 6.5.2.2-11], though the implementation (GCC vs.
clang) is free to choose which evaluation order to use. That's why you see
these differences between GCC and clang.

Andi

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

end of thread, other threads:[~2010-06-07 11:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-07  7:47 “x--” would self-add before opration "--" ? pem
2010-06-07  9:33 ` Paolo Bonzini
2010-06-07 11:06 ` pem
2010-06-07 11:40 ` “x--” would self-add before opration "-- " ? Andi Hellmund

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