[gnu.gcc.bug] sizeof <extern array>

raulmill@usc.edu (Raul Deluth Rockwell) (12/08/89)

gcc 1.36 barfs on usages of form:

#include <stdio.h>

extern foo[];

main()
{
    printf("%d\n", sizeof foo);	/* line 8 */
}

int foo[] = { 0, 1, 2, 3, };

as follows:

make 
gcc   -sun4 -c  test.c
test.c: In function main:
test.c:8: invalid use of array with unspecified bounds
*** Error code 1
make: Warning: Target `test' not remade because of errors

Compilation finished at Thu Dec  7 13:25:56

----------------------------------------------------------------------

This example is trivial, but essentially, you aren't allowed to use
sizeof on arrays stored in other source files.

There are a few work arounds, but they can be tedious to implement.

[ 0: put all references to arrays after the declarations of the
     arrays. 

  1: explicitly write in the array size in the extern declaration,
     change it every time the compiler barfs.

  2: put arrays in a special source file which when a flag is set
     build macros yeilding at the end a little gem which is the number
     of elements in the array. ]

I see no reason for this behavior in my ansi docs.  The only thing
I've found in the gcc docs is the sentence:

     The length of an array is computed on entry to the brace-level
     where the array is declared and is remembered for the scope of
     the array in case you access it with sizeof.

Even this doesn't apply to top-level declarations.

Raul Rockwell
INTERNET:   raulmill@usc.edu                       !
UUCP:       ...uunet!usc!raulmill                  !  55 mph = 82 nc
U.S.SNAIL:  721 E Windsor #4,  GLENDALE CA  91205  !

--