[Bug c/17253] New: Inconsistency in reading array indexes
vanveghel at home dot nl
gcc-bugzilla@gcc.gnu.org
Tue Aug 31 19:50:00 GMT 2004
Consider the following two snippets:
-- snip snip --
#include <stdio.h>
#include <string.h>
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 <stdio.h>
#include <string.h>
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
More information about the Gcc-bugs
mailing list