From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15060 invoked by alias); 12 Oct 2012 11:21:02 -0000 Received: (qmail 14988 invoked by uid 48); 12 Oct 2012 11:20:40 -0000 From: "yangzhe1990 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/54907] post increasing a value pointed by p in subexpression of an expression modifying p saves the increased value in the wrong place Date: Fri, 12 Oct 2012 11:21:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: yangzhe1990 at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-10/txt/msg01174.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D54907 --- Comment #2 from yangzhe1990 at gmail dot com 2012-10-12 11:20:39 UTC --- No, p is not modified twice. p is modified once, *p is modified once. (In reply to comment #1) > Not a bug p is modified twice without a seqence point the result is undef= ined >=20 >=20 >=20 > ________________________________ > From: yangzhe1990 at gmail dot com > To: gcc-bugs@gcc.gnu.org=20 > Sent: Friday, 12 October 2012, 11:10 > Subject: [Bug c/54907] New: post increasing a value pointed by p in > subexpression of an expression modifying p saves the increased value in t= he > wrong place >=20 >=20 > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D54907 >=20 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Bug #: 54907 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Summary: post increasing a value point= ed by p in subexpression > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 of = an expression modifying p saves the increased value > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 in = the wrong place > =C2=A0 =C2=A0 Classification: Unclassified > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Product: gcc > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Version: 4.7.1 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Status: UNCONFIRMED > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Severity: normal > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Priority: P3 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Component: c > =C2=A0 =C2=A0 =C2=A0 =C2=A0 AssignedTo: unassigned@gcc.gnu.org > =C2=A0 =C2=A0 =C2=A0 =C2=A0 ReportedBy: yangzhe1990@gmail.com >=20 >=20 > #include >=20 > int main() { > =C2=A0 =C2=A0 char s[] =3D "axxxxx"; > =C2=A0 =C2=A0 char *p =3D s; >=20 > =C2=A0 =C2=A0 printf("s =3D %s in the beginning.\n" > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "p is pointed at the %d-th char.\n", s= , p - s); > =C2=A0 =C2=A0 //p =3D p + (*p)++ * 3 + 2 - 'a' * 3; // (1) > =C2=A0 =C2=A0 p +=3D (*p)++ * 3 + 2 - 'a' * 3; // (2) > =C2=A0 =C2=A0 printf("p is moved ahead by %d steps\n", p - s); > =C2=A0 =C2=A0 printf("s =3D %s after the operation.\n", s); > =C2=A0 =C2=A0 return 0; > } >=20 > The expected result is "bxxxxx". But the output is "axbxxx". >=20 > Maybe in the wrong code, when it saves the value, it lookups the address = again > by *p, but p is modified in the expression. >=20 > As discussed in stackoverflow, > http://stackoverflow.com/questions/12823663/would-p-p-p-3-c-cause-an-unde= fined-behavior?answertab=3Dvotes#tab-top > most people think it's a bug of gcc. >=20 > Bug found in gcc 4.4.6, 4.7.1, g++ 4.4.6. g++ 4.7.1 produces the correct > result.