comp.lang.ada
 help / color / mirror / Atom feed
From: nobody@REPLAY.COM (Anonymous)
Subject: Re: Initialized global data
Date: 1998/05/06
Date: 1998-05-06T00:00:00+00:00	[thread overview]
Message-ID: <199805061414.QAA14832@basement.replay.com> (raw)
In-Reply-To: 6iosc2$3mp@bgtnsc01.worldnet.att.net


On Tue, 05 May 1998 22:24:45 -0700, Kevin Wells
<wellsk@worldnet.att.net> wrote:

> When specifying data in an Ada package that is pre-initialized such as
> ----------------------------------------
> package Generate_Matrix is
>   Scale_Factor : float := 10.903;
>   .
> end Generate_Matrix; -- spec
> ----------------------------------------
> 
> 1. Is it normal for the compiler to generate code that sets the value in
> memory
>     (that gets executed when the runtime system is initialized)?

Yes.

> 2. Is there a way to get around this?

You might try pragma Preelaborate (Ada only; not available in Ada 83).

> 
> I am trying to create pre-initialized data arrays in memory such as
> ----------------------------------------
> type Array_Type is array (1 .. 5) of integer;
> Small_Array : Array_Type := (1, 10, 5, 9, -50);
> ----------------------------------------
> but the compiler generates a bunch of code to set those 5 memory
> locations.
> ...

The general rule for global variables is: Never use global variables.

If these are not actually variables, they should be constants:

Scale_Factor : constant := 10.903; -- OR ": constant Float :="

Small_Array : constant Array_Type := (1, 10, 5, 9, -50);

In the case of named numbers (X : constant := 7.3;), the compiler should
not generate any code or allocate any memory. In the case of typed
constants (X : constant T := V;), both may occur. Pragma Preelaborate
might take care of this for you.

If the numbers are in ROM, you have to be able to get around this. You
might be able to use a deferred constant with a pragma Import (Ada
only):

X : constant T;
pragma Import (Convention, X, "Name_Of_X");

Ask your compiler vendor.

Or you can use an uninitialized "variable" (that doesn't) at a specified
address:

X : T;
for X'Address use A; -- Ada 83: "for X use at A;"

The compiler might then warn you that you are accessing undefined
values, but you know you can ignore these warnings.

Jeff Carter  PGP:1024/440FBE21
My real e-mail address: ( carter @ innocon . com )
"I waggle my private parts at your aunties."
Monty Python & the Holy Grail

Posted with Spam Hater - see
http://www.compulink.co.uk/~net-services/spam/




  parent reply	other threads:[~1998-05-06  0:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-05-05  0:00 Initialized global data Kevin Wells
1998-05-05  0:00 ` A little more data Kevin Wells
1998-05-06  0:00   ` Stephen Leake
1998-05-06  0:00 ` Anonymous [this message]
1998-05-08  0:00   ` Initialized global data Simon Wright
1998-05-08  0:00     ` Tucker Taft
1998-05-09  0:00       ` Simon Wright
1998-05-10  0:00         ` Robert Dewar
1998-05-11  0:00           ` Simon Wright
1998-05-11  0:00             ` Matthew Heaney
1998-05-06  0:00 ` John English
1998-05-06  0:00 ` Tucker Taft
1998-05-09  0:00 ` Niklas Holsti
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox