This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

What does zero-length array mean at file scope?


    Hi everyone,

  I found something rather odd in testcase gcc.c-torture/execute/20030811-1.c:

> /* Origin: PR target/11535 from H. J. Lu <hjl@lucon.org> */
> 
> void vararg (int i, ...)
> {
>   (void) i;
> }
> 
> int i0[0], i1;

  Huh?

> void test1 (void)
> {
>   int a = (int) (long long) __builtin_return_address (0);
>   vararg (0, a);
> }
> 
> void test2 (void)
> {
>   i0[0] = (int) (long long) __builtin_return_address (0);
> }

  Nasal demons?

> void test3 (void)
> {
>   i1 = (int) (long long) __builtin_return_address (0);
> }
> 
> void test4 (void)
> {
>   volatile long long a = (long long) __builtin_return_address (0);
>   i0[0] = (int) a;
> }

  And again.

  ELF GAS/LD seem happy enough when presented with a ".comm foo,0" directive,
but PE does rather literally what you asked, and gives you no data object,
leading to i0 in the above being an undefined reference at link time.

  What is supposed to happen here?  How much space is the compiler meant to
allocate to i0?  Where does the data go when it gets stored to i0[0]?

  I've read http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html about six times
and can't see anywhere it even hints that you can use this syntax anywhere
except as the trailing member of a struct.  When I look at the original
PR11535, the testcases in there are all similar but have array size [1] and
try to assign the builtin_return_address result to array[0] - that's clearly
valid, and quite different from how this testcase ended up.

  So, is the testcase invalid, and should the compiler be warning about this
declaration?  I couldn't get any complaint out of it even with "-W -Wall
-Wextra -pedantic -std=c89"?  Or should the linker be allocating some space
for this zero-sized common?

    cheers,
      DaveK


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]