rjnoe@riccb.UUCP (Roger J. Noe) (03/22/86)
I'm not sure if this is a cc bug or not. Suppose in a file I have the definition of a global array, e.g. int foo[43]; Suppose in another file I have a declaration of foo without the dimension and then refer to the size of the array: extern int foo[]; /* . . . */ sizeof foo / sizeof (int) /* . . . */ Then cc apparently "returns" zero for sizeof foo. Should this happen for an extern array? I know that if I stick in a dimension in the array declaration in the latter file: extern int foo[22]; then the compiler uses that value as the number of elements in the array rather than the number it was defined as having. We're running UNIX System V Release 2 on a VAX-11/785. -- Roger Noe ihnp4!riccb!rjnoe
gwyn@BRL.ARPA (VLD/VMB) (03/24/86)
sizeof is evaluated at compile time, not at link time or run time. extern int foo[]; ... sizeof foo ... should actually be reported as an error, since the compiler cannot possibly know the size of foo in this case. If you say instead extern int foo[22]; ... sizeof foo ... then the compiler will trust you and think that sizeof foo is 22 times sizeof(int). Strictly speaking it is an error to use that declaration in one place and the definition int foo[34]; in another place, although old PCC-based compilers/linkers that use the "named common" model for extern storage often permit this. C, particularly in some implementations, allows one to get away with much sloppiness, but that doesn't mean one should take unnecessary advantage of it. Think strict typing and you'll have much less trouble with C code, especially with portability.