diagnostic request

Geoff Keating geoffk@geoffk.org
Wed Feb 6 14:15:00 GMT 2002


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>



More information about the Gcc-bugs mailing list