The following code generates a "1" with gcc-4.1 and generates a "2" with a lot
of other compilers (Visual Studio, gcc-3.2, ...). I have read the non-bugs
section on http://gcc.gnu.org/bugs.html (also bug 11751) and I have read about
"sequence points" on http://c-faq.com/expr/seqpoints.html. Still I feel that
this is really a bug rather than a non-bug as in the many examples. Why?
Because a function call is also a sequence point.

Here is a small code snippet, that is compiled with no special settings:

#include <stdio.h>

int inca_and_ret1(int *p_a)
{
  ++(*p_a);
  return 1;
}

int main()
{
  int a = 0;
  int *p_a = &a;

  (*p_a) += inca_and_ret1(p_a);

  printf("%d\n", a);

  return 0;
}

The function call to inca_and_ret1 is a sequence point, this the value pointed
to by p_a should be correct after the call. And then the expression "(*p_a) +=
<return value>" should be evaluated (I think).

The example snippet I present here looks very simple and stupid, but I have a
similar situation in my code and there it is far less obvious. I stumbled
across this problem by accident and I have no clue how many of these problems
exist in my code (or other people's code).

The command "gcc -v -save-temps -o bug bug.c" gave the following output:

Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
 /usr/lib/gcc/i486-linux-gnu/4.1.2/cc1 -E -quiet -v bug.c -mtune=generic
-fpch-preprocess -o bug.i
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.1.2/include
 /usr/include
End of search list.
 /usr/lib/gcc/i486-linux-gnu/4.1.2/cc1 -fpreprocessed bug.i -quiet -dumpbase
bug.c -mtune=generic -auxbase bug -version -fstack-protector -fstack-protector
-o bug.s
GNU C version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) (i486-linux-gnu)
        compiled by GNU C version 4.1.2 (Ubuntu 4.1.2-0ubuntu4).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: c0d954aeefbb96d795ff3f6b3b72ef51
 as -V -Qy -o bug.o bug.s
GNU assembler version 2.17.50 (i486-linux-gnu) using BFD version 2.17.50
20070103 Ubuntu
 /usr/lib/gcc/i486-linux-gnu/4.1.2/collect2 --eh-frame-hdr -m elf_i386
--hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o bug
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crti.o
/usr/lib/gcc/i486-linux-gnu/4.1.2/crtbegin.o
-L/usr/lib/gcc/i486-linux-gnu/4.1.2 -L/usr/lib/gcc/i486-linux-gnu/4.1.2
-L/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib -L/lib/../lib
-L/usr/lib/../lib bug.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc
--as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i486-linux-gnu/4.1.2/crtend.o
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crtn.o


-- 
           Summary: Wrong evaluation
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tim dot bruylants at vub dot ac dot be
GCC target triplet: i486-linux-gnu


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

Reply via email to