This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: diagnostic request
- From: Geoff Keating <geoffk at geoffk dot org>
- To: phr-2002 at nightsong dot com
- Cc: gcc-bugs at gcc dot gnu dot org
- Date: 06 Feb 2002 13:53:20 -0800
- Subject: Re: diagnostic request
- References: <20020206111854.2994.qmail.cygnus.bug-gcc@brouhaha.com>
phr-2002@nightsong.com writes:
> The code
>
> int a[3] = {1, 2, 3};
> test() { a[a[0]]=1; }
>
> looks like it sets a[1] to 1. However, according to some intricate
> reading of the ANSI C standard, its behavior is apparently undefined--
> there's no sequence point after the computation of a[0].
Actually, that code has defined behaviour. The undefined version is
int a[3] = {0, 1, 2};
main() { a[a[0]]=1; }
> Apparently this is intended to allow vectorizing compilers to
> optimize array assignments by assuming the arrays aren't
> "self-modifying". I don't understand the subtleties and may have
> gotten the example slightly wrong, but there's a big thread about it
> in comp.std.c right now, that's spilled over into sci.crypt.
>
> I think GCC should compile the above code in whatever way it wants,
> but it should print a diagnostic warning of a possible bug.
Because the error is a run-time property, it's difficult for GCC to
consistently warn about this. For instance, you could also have
written
void test (int *a, const int *b)
{
a[b[0]] = 1;
}
and called it with
test(a, a);
and yet you probably don't want a warning for the above definition of
'test', since code like that is pretty common.
Or, you might happen to know that a[0] != 0, in which case the code is
also valid.
--
- Geoffrey Keating <geoffk@geoffk.org> <geoffk@redhat.com>