c/10143: Post increment doesn work more than once per statement
macdonn@nortelnetworks.com
macdonn@nortelnetworks.com
Wed Mar 19 01:14:00 GMT 2003
>Number: 10143
>Category: c
>Synopsis: Post increment doesn work more than once per statement
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: wrong-code
>Submitter-Id: net
>Arrival-Date: Wed Mar 19 01:06:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator: Nick MacDonald
>Release: gcc 3.2.2, gcc 2.95.2
>Organization:
>Environment:
SunOS 5.8 Generic_108528-18 sun4u sparc SUNW,Ultra-5_10
>Description:
The following code produces the following output... I do not believe this to be correct, and a sample output from MS Visual C++ is also attached. A second program (a debugging portion of the one that I used to originally find the problem, is also attached.)
Program one:
#include <stdio.h>
int main(int argc, char* argv[])
{
int i1, j1;
int i2, j2;
int i3, j3;
int i4, j4;
int i5, j5;
int i6, j6;
int i7, j7;
int t;
for (i1=0, j1=0; i1<10; t=i1++| i1++) j1++;
for (i2=0, j2=0; i2<10; t=i2++ * i2++) j2++;
for (i3=0, j3=0; i3<10; i3++, i3++) j3++;
for (i4=0, j4=0; i4<10; t=++i4| ++i4) ++j4;
for (i5=0, j5=0; i5<10; t=++i5 * ++i5) ++j5;
for (i6=0, j6=0; i6<10; ++i6, ++i6) ++j6;
for (i7=0, j7=0; i7<10; t=i7++| ++i7) j7++;
printf("i1=%d, j1=%d\n", i1, j1);
printf("i2=%d, j2=%d\n", i2, j2);
printf("i3=%d, j3=%d\n", i3, j3);
printf("\n");
printf("i4=%d, j4=%d\n", i4, j4);
printf("i5=%d, j5=%d\n", i5, j5);
printf("i6=%d, j6=%d\n", i6, j6);
printf("\n");
printf("i7=%d, j7=%d\n", i7, j7);
return 0;
}
Generated [incorrect] Results:
i1=10, j1=10
i2=10, j2=10
i3=10, j3=5
i4=10, j4=5
i5=10, j5=5
i6=10, j6=5
i7=10, j7=10
[What I consider to be] Correct results:
Here is the output from VC6 and WinNT.
i1=10, j1=5
i2=10, j2=5
i3=10, j3=5
i4=10, j4=5
i5=10, j5=5
i6=10, j6=5
i7=10, j7=5
Program 2:
#include <stdio.h>
int main(int argc, char* argv[])
{
unsigned char x[]={1,2,3,4};
unsigned long e, f, g, h, i;
int p=0, q=0, r=-1, s=0;
int a, b, c, d;
e=(x[p++]<<24) |
(x[p++]<<16) |
(x[p++]<< 8) |
(x[p++]);
f=(x[(a=q++)]<<24) |
(x[(b=q++)]<<16) |
(x[(c=q++)]<< 8) |
(x[(d=q++)]);
g=(x[++r]<<24) |
(x[++r]<<16) |
(x[++r]<< 8) |
(x[++r]);
h=(x[0]<<24) |
(x[1]<<16) |
(x[2]<< 8) |
(x[3]);
i =(x[s++]<<24);
i|=(x[s++]<<16);
i|=(x[s++]<< 8);
i|=(x[s++]);
printf("\n\
unsigned char x[]={1,2,3,4};\n\
unsigned long e, f, g, h, i;\n\
int p=0, q=0, r=-1, s=0;\n\
int a, b, c, d;\n\
\n\
e=(x[p++]<<24) |\n\
(x[p++]<<16) |\n\
(x[p++]<< 8) |\n\
(x[p++]);\n\
\n\
f=(x[(a=q++)]<<24) |\n\
(x[(b=q++)]<<16) |\n\
(x[(c=q++)]<< 8) |\n\
(x[(d=q++)]);\n\
\n\
g=(x[++r]<<24) |\n\
(x[++r]<<16) |\n\
(x[++r]<< 8) |\n\
(x[++r]);\n\
\n\
h=(x[0]<<24) |\n\
(x[1]<<16) |\n\
(x[2]<< 8) |\n\
(x[3]);\n\
\n\
i =(x[s++]<<24);\n\
i|=(x[s++]<<16);\n\
i|=(x[s++]<< 8);\n\
i|=(x[s++]);\n\
\n\n");
printf("p=%d e=%lu or 0x%08x\n", p, e, e);
printf("q=%d f=%lu or 0x%08x\n", q, f, f);
printf(" a=%d b=%d c=%d d=%d\n", a, b, c, d);
printf("r=%d g=%lu or 0x%08x\n", r, g, g);
printf (" h=%lu or 0x%08x\n", h, h);
printf("s=%d i=%lu or 0x%08x\n", s, i, i);
return 0;
}
The [incorrect] results of this program:
unsigned char x[]={1,2,3,4};
unsigned long e, f, g, h, i;
int p=0, q=0, r=-1, s=0;
int a, b, c, d;
e=(x[p++]<<24) |
(x[p++]<<16) |
(x[p++]<< 8) |
(x[p++]);
f=(x[(a=q++)]<<24) |
(x[(b=q++)]<<16) |
(x[(c=q++)]<< 8) |
(x[(d=q++)]);
g=(x[++r]<<24) |
(x[++r]<<16) |
(x[++r]<< 8) |
(x[++r]);
h=(x[0]<<24) |
(x[1]<<16) |
(x[2]<< 8) |
(x[3]);
i =(x[s++]<<24);
i|=(x[s++]<<16);
i|=(x[s++]<< 8);
i|=(x[s++]);
p=1 e=16843009 or 0x01010101
q=1 f=16843009 or 0x01010101
a=0 b=0 c=0 d=0
r=3 g=16909060 or 0x01020304
h=16909060 or 0x01020304
s=4 i=16909060 or 0x01020304
[p and q should not be 1, and b,c,d should not be 0.]
>How-To-Repeat:
Compile the supplied program and run it. Correct (IMHO) results should look like:
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="WeirdForLoop.c"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="WeirdForLoop.c"
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciogYXJndltdKQp7CiAg
aW50IGkxLCBqMTsKICBpbnQgaTIsIGoyOwogIGludCBpMywgajM7CiAgaW50IGk0LCBqNDsKICBp
bnQgaTUsIGo1OwogIGludCBpNiwgajY7CiAgaW50IGk3LCBqNzsKICBpbnQgdDsKCiAgZm9yIChp
MT0wLCBqMT0wOyBpMTwxMDsgdD1pMSsrfCBpMSsrKSAgajErKzsKICBmb3IgKGkyPTAsIGoyPTA7
IGkyPDEwOyB0PWkyKysgKiBpMisrKSBqMisrOwogIGZvciAoaTM9MCwgajM9MDsgaTM8MTA7IGkz
KyssIGkzKyspICAgIGozKys7CgogIGZvciAoaTQ9MCwgajQ9MDsgaTQ8MTA7IHQ9KytpNHwgKytp
NCkgICsrajQ7CiAgZm9yIChpNT0wLCBqNT0wOyBpNTwxMDsgdD0rK2k1ICogKytpNSkgKytqNTsK
ICBmb3IgKGk2PTAsIGo2PTA7IGk2PDEwOyArK2k2LCArK2k2KSAgICArK2o2OwoKICBmb3IgKGk3
PTAsIGo3PTA7IGk3PDEwOyB0PWk3Kyt8ICsraTcpICBqNysrOwoKICBwcmludGYoImkxPSVkLCBq
MT0lZFxuIiwgaTEsIGoxKTsKICBwcmludGYoImkyPSVkLCBqMj0lZFxuIiwgaTIsIGoyKTsKICBw
cmludGYoImkzPSVkLCBqMz0lZFxuIiwgaTMsIGozKTsKICBwcmludGYoIlxuIik7CiAgcHJpbnRm
KCJpND0lZCwgajQ9JWRcbiIsIGk0LCBqNCk7CiAgcHJpbnRmKCJpNT0lZCwgajU9JWRcbiIsIGk1
LCBqNSk7CiAgcHJpbnRmKCJpNj0lZCwgajY9JWRcbiIsIGk2LCBqNik7CiAgcHJpbnRmKCJcbiIp
OwogIHByaW50ZigiaTc9JWQsIGo3PSVkXG4iLCBpNywgajcpOwoKICByZXR1cm4gMDsKfQoK
More information about the Gcc-bugs
mailing list