From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7778 invoked by alias); 31 Aug 2004 19:50:46 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 7771 invoked by uid 48); 31 Aug 2004 19:50:45 -0000 Date: Tue, 31 Aug 2004 19:50:00 -0000 From: "vanveghel at home dot nl" To: gcc-bugs@gcc.gnu.org Message-ID: <20040831195044.17253.vanveghel@home.nl> Reply-To: gcc-bugzilla@gcc.gnu.org Subject: [Bug c/17253] New: Inconsistency in reading array indexes X-Bugzilla-Reason: CC X-SW-Source: 2004-08/txt/msg03303.txt.bz2 List-Id: Consider the following two snippets: -- snip snip -- #include #include int main (void) { char test[5][5]; int j = 3; memset (&test, ' ', 5 * 5); test[j][j++] = '!'; printf ("test[3][3] = %c\ntest[4][3] = %c\n", test[3][3], test[4][3]); return 0; } -- snip snip -- And: -- snip snip -- #include #include int main (void) { char test[5][5]; int j = 3; memset (&test, ' ', 5 * 5); test[(j == 4) ? 4 : 3][j++] = '!'; printf ("test[3][3] = %c\ntest[4][3] = %c\n", test[3][3], test[4][3]); return 0; } -- snip snip -- Running both these snippets produces the following results with GCC 3.3.4 running on Debian GNU/Linux Unstable (x86): $ ./test test[3][3] = test[4][3] = ! $ ./test2 test[3][3] = ! test[4][3] = $ It seems that j++ is evaluated first only in the second case, making the statement in the first bracket evaluate to 4. This may be a result of 'undefined behavior' caused by sequence points, and indeed, GCC does issue a warning ("warning: operation on `j' may be undefined"), but it does seem like something that can at least be improved. Kind Regards, Jasper -- Summary: Inconsistency in reading array indexes Product: gcc Version: 3.3.4 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: vanveghel at home dot nl CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: 3.3.4 GCC host triplet: 3.3.4 GCC target triplet: 3.3.4 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17253