sizeof extern arrays

VLD/VMB gwyn at BRL.ARPA
Mon Mar 24 15:41:48 AEST 1986


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.



More information about the Comp.lang.c mailing list