[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::c_plus_plus

Title:C++
Notice:Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS)
Moderator:DECCXX::AMARTIN
Created:Fri Nov 06 1987
Last Modified:Thu Jun 05 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3604
Total number of notes:18242

3477.0. "Storage for integer constants" by VAXCPU::michaud (Jeff Michaud - ObjectBroker) Wed Mar 05 1997 22:44

	Someone in my group has defined a couple of simple constants
	using something like:

const int FOO = 7;

	in a header file, instead of using a preprocessor macro or an
	enum, which btw, using const vars for this purpose is suggested
	in the ARM (section 7.1.6).

	While this, thanks to the default scope being file local, works
	fine, my concern is wasted storage, as in theory each module which
	includes the header file will allocate (internal linkage) storage for 
	FOO.  This appears to be the case even if FOO is used only as a
	constant, and it's address never taken (see enclosed generated
	assembler), so the generated code never even references the storage.
	Will the unused storage be optimized out when linked?

	The only alternative that appears to work is to put:

extern const int FOO;

	in the header file, and:

extern const int FOO = 7;

	in a module to declare storage for it only once.  But that results
	in, as expected, the compiler generating code that references the
	external memory in all the other modules, and hence it's not equiv
	to using a constant any longer (especially if you try using it
	where a constant expression is required).

	.ugen	
	.verstamp	3 11
	.rconst	
	.align	3
	.align	0
$$3:
	.long	7 : 1
	.text	
	.align	4
	.file	2 "one.cxx"
	.globl	routine__Xv
	.loc	2 0

	.ent	routine__Xv 2
routine__Xv:
	.option	O2
	ldgp	$gp, 0($27)
	.frame	$sp, 0, $26, 0
	.prologue	1
	.loc	2 3
 #    1	#include "foo.hxx"
 #    2	
 #    3	int
	.loc	2 5
 #    4	routine()
 #    5	{
	.loc	2 6
 #    6	    return FOO;
	ldil	$0, 7
	.livereg	0xFC7F0002,0x3FC00000
	ret	$31, ($26), 1
	.end	routine__Xv
T.RTitleUserPersonal
Name
DateLines
3477.1NotedDECCXX::AMARTINAlan H. MartinThu Mar 06 1997 17:382
Thanks; I've noted this for future optimization.
				/AHM