Re: Self-referring const array initialisation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Hamilton, Ian" <Ian.Hamilton@xxxxxxxxxxxxx> writes:

> Hi there
> 
> Here is a test program to illustrate a problem in some code
> I have inherited:
> 
> #include <stdio.h>
> 
> const int array1[5]=
> {
>   1,
>   2,
>   3,
>   4,
>   5
> };
> 
> const int array2[5]=
> {
>   1,
>   array2[0] + 1,
>   array2[1] + 1,
>   array2[2] + 1,
>   array2[3] + 1,
> };
> 
> int main()
> {
>   printf ("Array values:\n\n");
>   printf("array1  array2\n");
> 
>   printf("  %i       %i\n", array1[0], array2[0]);
>   printf("  %i       %i\n", array1[1], array2[1]);
>   printf("  %i       %i\n", array1[2], array2[2]);
>   printf("  %i       %i\n", array1[3], array2[3]);
>   printf("  %i       %i\n", array1[4], array2[4]);
> }
> 
> 
> If I build and run this on a Sun/Sparc/Solaris system using
> gcc version 2.95.2, I get the following output:
> 
> Array values:
> 
> array1  array2
>   1       1
>   2       2
>   3       3
>   4       4
>   5       5
> 
> 
> However, if I use gcc version 3.3.2, I get this output:
> 
> Array values:
> 
> array1  array2
>   1       1
>   2       1
>   3       1
>   4       1
>   5       1

FWIW, on i386-freebsd5.2, gcc 3.4 release cannidate gives the same
    output as gcc 2.95.2 . So I think this bug is fixed in 3.4 (but it is
    still present in 3.3.3). I can't find the bug report, however.

[snip]
> My question is whether this sort of self-reference is allowed by
> the C and/or C++ standards. In other words, is this a compiler bug,
> or a bug in the above code?

I am nearly certain C++ allows this. I can't find anything in 8.5.1
    that disallows it.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux