Does extern "const" allocate storage?

Daniel A. Glasser dag at chinet.UUCP
Thu Mar 31 19:29:02 AEST 1988


In article <3241 at haddock.ISC.COM> karl at 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 at chinet.UUCP
    One of those things that goes "BUMP!!! (ouch!)" in the night.
 ...!att-ih!chinet!dag | ...!ihnp4!mwc!dag | ...!ihnp4!mwc!gorgon!dag



More information about the Comp.lang.c mailing list