public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: macdonn@nortelnetworks.com
To: gcc-gnats@gcc.gnu.org
Subject: c/10143: Post increment doesn work more than once per statement
Date: Wed, 19 Mar 2003 01:06:00 -0000	[thread overview]
Message-ID: <20030319010050.10841.qmail@sources.redhat.com> (raw)


>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


             reply	other threads:[~2003-03-19  1:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-19  1:06 macdonn [this message]
2003-03-19  7:10 neil
2003-03-19 14:06 Nick Macdonald
2003-03-19 14:26 Falk Hueffner
2003-03-19 19:06 Joseph S. Myers
2003-03-19 19:56 Nick Macdonald
2003-03-19 20:16 Joseph S. Myers
2003-03-19 22:36 Nick Macdonald
2003-03-20  0:16 Neil Booth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030319010050.10841.qmail@sources.redhat.com \
    --to=macdonn@nortelnetworks.com \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).