From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12912 invoked by alias); 12 Oct 2012 10:11:20 -0000 Received: (qmail 12821 invoked by uid 48); 12 Oct 2012 10:10:54 -0000 From: "yangzhe1990 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/54907] New: 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 10:11:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new 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: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" 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/msg01169.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54907 Bug #: 54907 Summary: post increasing a value pointed by p in subexpression of an expression modifying p saves the increased value in the wrong place Classification: Unclassified Product: gcc Version: 4.7.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned@gcc.gnu.org ReportedBy: yangzhe1990@gmail.com #include int main() { char s[] = "axxxxx"; char *p = s; printf("s = %s in the beginning.\n" "p is pointed at the %d-th char.\n", s, p - s); //p = p + (*p)++ * 3 + 2 - 'a' * 3; // (1) p += (*p)++ * 3 + 2 - 'a' * 3; // (2) printf("p is moved ahead by %d steps\n", p - s); printf("s = %s after the operation.\n", s); return 0; } The expected result is "bxxxxx". But the output is "axbxxx". Maybe in the wrong code, when it saves the value, it lookups the address again by *p, but p is modified in the expression. As discussed in stackoverflow, http://stackoverflow.com/questions/12823663/would-p-p-p-3-c-cause-an-undefined-behavior?answertab=votes#tab-top most people think it's a bug of gcc. Bug found in gcc 4.4.6, 4.7.1, g++ 4.4.6. g++ 4.7.1 produces the correct result.