William F Mitchell wrote:
>Van Snyder wrote:
> >
> > Aleksandar Donev asked:
> >
> > > I am wondering if anyone can clarify the effect of the SAVE attribute
> > > for global data (i.e. data declared in modules). For example:
> > >
> > > program main
> > > use routines, only : routine
> > > call routine()
> > > ...
> > > call routine()
> > > end program main
> > >
> > > module routines
> > > use shared, only : value
> > > contains
> > > subroutine routine()
> > > write(*,*) value
> > > value=value+1
> > > end subroutine routine
> > > end module routines
> > >
> > > module shared
> > > integer, SAVE :: value
> > > end module shared
> > >
> > > Does it matter that there is a SAVE on value. If not, when does it
> > > matter.
> >
> > The standard allows non-saved module variables to disappear when there
> > is no procedure accessing the module by use association. In your case,
> > any nonsaved variables in the "shared" module could in principle disappear
> > between calls to "routine" because there is no "use shared" in your main
> > program. I don't know if any compilers take advantage of this feature
> > of the standard.
> >
> > Best regards,
> > Van Snyder
>
>Maybe I'm missing something here, but I thought that that access includes
>indirect
>access, i.e. "main" uses "routines" which uses "shared", so then "value"
>is still in
>scope in the main program and remains defined without the save attribute. In
>fact, we could have a "print *, value" statement in main, right? Section
>11.3.2
>page 188 of the f95 standard says "If the local name [of an entity made
>accessible
>by a USE statement] does not appear in either a PUBLIC or PRIVATE
>statement, it
>assumes the default accessibility attribute of that scoping unit." Am I
>misinterpreting
>this? Doesn't this make "value" accessible in "main"?
>
>Bill
1. Van is incorrect. Because main uses module routines and routines uses
module shared, everything in shared (i.e., the variable value) has a
lifetime at least as long as that of the main program and thus would behave
no differently if the SAVE attribute were removed. However, if routine
were and external procedure rather than a module procedure, so main would
not be indirectly using module shared, Van's analysis would be correct,
although I know of no implementations in which value actually disappears.
2. Bill is also incorrect. He is correct that value is PUBLIC in module
routines, but since main says "only: routine" when it uses module routines,
value is _not_ accessible in main.
To the best of my knowledge, no current implementations of f90 or f95 treat
COMMON or module variables without the SAVE attribute any differently from
those with it. However, the standard allows there to be differences. With
the SAVE attribute, a module variable can be used to communicate from a
scoping unit that uses that module to any other scoping unit using that
module, regardless of where it appears in the call tree. Without the SAVE
attribute, you can, in effect, communicate only up and down the call
tree. A possible benefit of this restriction is that in a parallel
programming environment, such independent communication might allow a
processor to put a separate copy of the unSAVEd part of a module on each
processor in contrast to the synchronizing with a single shared copy it
must do with the SAVEd part of the module.
--
Kurt W Hirchert [log in to unmask]
UIUC Department of Atmospheric Sciences +1-217-265-0327
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|