[comp.lang.c++] Why put constants on the stack?

kenny@uiucdcsb.cs.uiuc.edu (05/27/87)

Running 1.1 (so I don't know whether this has been fixed yet):

When a local variable is declared ``const,'' the compiler appears to
assume that it is automatic as well.  This leads to the following
anomaly:
-------------------- bad.c --------------------
main () {
	const int array [] = { 1, 2, 3 };
	}
CC  bad.c:
"bad.c", line 3: sorry, not implemented: initialization of automatic aggregates
"bad.c", line 3: sorry, not implemented:  initializer list for local variable array
2 errors
-------------------- good.c -------------------
main () {
	static const int array [] = { 1, 2, 3 };
	}
(compiles cleanly)
-----------------------------------------------
Why are ``const'' variables (other than parameters, of course) not
assumed to be static?  In addition to the above annoyance, it seems
like a pointless waste of stack space.  Have I overlooked something?

Kevin Kenny			UUCP: {ihnp4,pur-ee,convex}!uiucdcs!kenny
Department of Computer Science	ARPA: kenny@B.CS.UIUC.EDU (kenny@UIUC.ARPA)
University of Illinois		CSNET: kenny@UIUC.CSNET
1304 W. Springfield Ave.
Urbana, Illinois, 61801		Voice: (217) 333-8740

ark@alice.UUCP (06/07/87)

In article <165700009@uiucdcsb>, kenny@uiucdcsb.UUCP writes:
> Why are ``const'' variables (other than parameters, of course) not
> assumed to be static?  In addition to the above annoyance, it seems
> like a pointless waste of stack space.  Have I overlooked something?

Saying a variable is a ``const'' is saying that its value won't change
over its lifetime.  It doesn't promise to initialize it to a constant.
There's no problem in saying

	int n;
	cin >> n;
	const k = n * 2;

meissner@dg_rtp.UUCP (06/15/87)

	/* somebody else from uiucdcsb */
> When a local variable is declared ``const,'' the compiler appears to
> assume that it is automatic as well.

> Why are ``const'' variables (other than parameters, of course) not
> assumed to be static?


In article <354@formtek.UUCP> kls@formtek.UUCP (Karl Swartz) writes:
> In dpANSI C, const functions as a type-modifier (even though) they
> don't call it that.  Thus, "const int foo" has no explicit storage-
> class and is therefore assumed to be automatic (within a function
> definition); the "const" has no effect in this regard.
> 
> Perhaps C++ is thinking along the same lines?

Ughhhh, it was the other way around, dpANSI adopted const from C++ (and
then added volatile to allow optization on memory mapped I/O).
-- 
	Michael Meissner, Data General	Uucp: ...mcnc!rti!dg_rtp!meissner

It is 11pm, do you know what your sendmail and uucico are doing?