[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Miscompiled statement a[i] = (a[i]++) % x;



>Submitter-Id:  net
>Originator:    Gregor Jasny
>Confidential:  no
>Synopsis:      Miscompiled statement a[i] = (a[i]++) % x;
>Severity:      non-critical
>Priority:      low
>Class:      	c
>Category:      wrong-code
>Release:       gcc (GCC) 4.1.3 20070718 (prerelease) (Debian 4.1.2-14)
>Environment:
System: Linux Mini 2.6.22 #1 SMP Tue Jul 10 12:09:18 CEST 2007 i686 GNU/Linux

host: i486-pc-linux-gnu
build: i486-pc-linux-gnu
target: i486-pc-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 --with-gxx-include-dir=/usr/include/c++/4.1.3 --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu
>Description:
When the following program is compiled with gcc the output of the array-value is 1 2 3 4 5 6. Icc and cl produce the output 1 2 3 4 1 2. If the arrray is susbtituted by a simple scalar, the gcc output is 1 2 3 4 1 2, too.

I don't know if the statement if even valid and behavior-defined C code. If it's not, then gcc maybe should warn somehow.

Thanks,
        Gregor

PS: This hapens with gcc-snapshot 20070720-1, too.

>How-To-Repeat:
#include <stdio.h>

int main()
{
        unsigned char a[42], b;
        int i, idx = 1;

        for (i=0; i<6; i++)
        {
                a[idx] = i;
                a[idx] = (a[idx]++) % 4;

                b = i;
                b = (b++) % 4;

                printf("i: %i %i\n", a[idx], b);
        }

        return 0;
}



Reply to: