- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a sub in a module. the sub have a local variable. the first time the main program call the sub, I initiate the local variable. then I call the sub the second time without initiation, but the local variable value remain the same.
[fortran]
module testmod
implicit none
type :: testtype
integer :: i
end type testtype
contains
subroutine foo(switch)
logical, intent(in) :: switch
type(testtype) :: t
integer :: m
if(switch==.TRUE.) then
t%i = 10
m = 23
end if
write(*,*) t%i, m
end subroutine foo
end module testmod
[/fortran]
[fortran]
program main
use testmod
implicit none
integer :: i
call foo(.TRUE.)
call foo(.FALSE.)
end program main
[/fortran]
I didn't turn on the /Qsave. I want to know is this feature just a compiler hidden rule and I should not rely on it?
thank you !!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here it is just coincidence that the same memory address was used and that nothing had written to that address in between calls. Don't depend on this. If you want to test this, put a PRINT statement between the calls. You'll find that m now has a different value. t%i may be the same because by default the compiler uses static storage for derived type variables, but this still doesn't make it SAVE.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can we assume that RECURSIVE or compilation options implying /Qauto would change away from static storage?
As Steve said, it is possible that the data remain visible between calls if no intervening function call uses the space, but that's an inadequately predicable side effect.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, RECURSIVE or /Qauto (or /Qopenmp) will change this default.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page