Path: utzoo!utgpu!water!watmath!clyde!att-cb!att-ih!chinet!dag From: dag@chinet.UUCP (Daniel A. Glasser) Newsgroups: comp.lang.c Subject: Re: Does extern "const" allocate storage? Message-ID: <4381@chinet.UUCP> Date: 31 Mar 88 09:29:02 GMT References: <7712@apple.Apple.Com> <3034@haddock.ISC.COM> <613@mcrware.UUCP> <3117@haddock.ISC.COM> <3938@chinet.UUCP> <3241@haddock.ISC.COM> Reply-To: dag@chinet.UUCP (Daniel A. Glasser) Organization: Chinet - Public Access Unix Lines: 54 In article <3241@haddock.ISC.COM> karl@haddock.ima.isc.com (Karl Heuer) writes: [discussion and excerpts about const/storage and the ANSI docs. removed] >I think it's covered by the as-if rule. If I say "int const x=5;", then since >we seem to agree that the value can never change (otherwise the implementation >would not have the liberty to enROM it), a compiler ought to be able to >convert "return x;" into "return 5;". To disprove this, you'd have to come up >with a strictly conforming program in which the optimized version produces a >different result than the non-optimized. ---- 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, though I consider this rather unsavory behavior, but it is not at liberty to omit storage for it entirely. I cannot remember if ANSI C has const functions. (A const function is one which has exactly one (possibly non-unique) value for each possible argument value, thus the expression extern int foo(const int); int i, a; i = foo(a)*foo(a); can be converted by the compiler to (for a hypothetical machine) (function return in reg0) push a_ call foo_ pop mult reg0,reg0 store reg0, i_ In some cases, this optimization is a REAL win! -- Daniel A. Glasser dag@chinet.UUCP One of those things that goes "BUMP!!! (ouch!)" in the night. ...!att-ih!chinet!dag | ...!ihnp4!mwc!dag | ...!ihnp4!mwc!gorgon!dag