Does extern "const" allocate storage?

Karl Heuer karl at haddock.ISC.COM
Sat Apr 2 05:54:33 AEST 1988


In article <4381 at chinet.UUCP> dag at chinet.UUCP (Daniel A. Glasser) writes:
>---- begin file 1 -----
>  const int x = 5;
>  int foo() { return x; }
>---- begin file 2 -----
>  extern const int x;
>  extern int foo();
>  int fie() { return x == foo(); }
>---- end example -----
>Maybe the compiler is at liberty to substitute the value 5 for the use of x
>in the function foo [but it isn't allowed to omit the storage entirely].

I think we're in agreement now.  What I claim is that (a) the substitution is
valid if the compiler knows the value, and (b) if *all* possible references
are so optimized, then the compiler need not allocate the space.  This is
normally possible only if the object has non-external linkage.

Actually, it's theoretically possible even in your example, if the compiler is
willing to do cross-file optimization.  (It would also have to "know" that no
library routines depend on x.)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list