Home > Archive > Fortran > July 2004 > Initialization in modules
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Initialization in modules
|
|
| Drew McCormack 2004-07-21, 3:58 pm |
| I have two questions about variables in modules:
1) Is it legal to initialize a variable in a module? I assumed it was,
but we are having compiler troubles, and reading the Metcalf book just
me even more.
(It states that you are not allowed to initialize a variable which is
use or host associated. I assume this means you are not allowed to
initialize a module variable outside of the data block of the module. I
have no idea how you could even do that.)
Here is an example to demonstrate. Is it legal?
module Blah
real :: variable = 1.e-8
end module
2) Does giving the 'save' attribute to module variables make any sense?
I would think module variables are automatically saved, but others are
trying to convince me otherwise. This is what they suggest you need to
do in the example above:
module Blah
real, save :: variable = 1.e-8
end module
Thanks in advance,
Drew McCormack
| |
| Ian Bush 2004-07-21, 3:58 pm |
|
Hi Drew,
Drew McCormack wrote:
> I have two questions about variables in modules:
>
>
> 1) Is it legal to initialize a variable in a module? I assumed it was,
> but we are having compiler troubles, and reading the Metcalf book just
> me even more.
>
> Here is an example to demonstrate. Is it legal?
>
> module Blah
> real :: variable = 1.e-8
> end module
>
Yup, that's fine.
>
> 2) Does giving the 'save' attribute to module variables make any sense?
> I would think module variables are automatically saved, but others are
> trying to convince me otherwise. This is what they suggest you need to
> do in the example above:
>
> module Blah
> real, save :: variable = 1.e-8
> end module
>
Yes it does make sense, and no module variables are not automatically
saved. There was part of a thread on this just a few days ago. Have a
look through the one entitled "Control host association",
Ian
| |
| Jugoslav Dujic 2004-07-21, 3:58 pm |
| Drew McCormack wrote:
| I have two questions about variables in modules:
|
|
| 1) Is it legal to initialize a variable in a module? I assumed it was,
| but we are having compiler troubles, and reading the Metcalf book just
| me even more.
|
| (It states that you are not allowed to initialize a variable which is
| use or host associated. I assume this means you are not allowed to
| initialize a module variable outside of the data block of the module. I
| have no idea how you could even do that.)
|
| Here is an example to demonstrate. Is it legal?
|
| module Blah
| real :: variable = 1.e-8
| end module
Yes, it's 100% legal.
| 2) Does giving the 'save' attribute to module variables make any sense?
| I would think module variables are automatically saved, but others are
| trying to convince me otherwise. This is what they suggest you need to
| do in the example above:
|
| module Blah
| real, save :: variable = 1.e-8
| end module
You're both right in a sense. They are tad more right though.
First: In F90 onwards, initialization implies SAVE. Thus, in the
example above, SAVE is implied, and explicit spelling is
redundant (but doesn't do harm either).
Second: If you don't use SAVE in a module (you can put it just
as first separate statement in the module), there are NO guarantees
that variables' values will be persistent through their lifetime
(except the explicitly SAVEd or initialized ones, as above).
Thus, they're right. In practice, every sane* compiler on
Earth will save module variables whether you explicitly tell it or
not, so you're likely to get by without SAVE. As usual, spelling
it out is a nice practice.
*) it's legal to not use such implicit save, but it's highly impractical
for any implementation on current computer architectures. Days of
overlays are (hopefully) long gone.
--
Jugoslav
___________
www.geocities.com/jdujic
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
|
|
|
|
|