Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

the local variable is auto save ?

史_建鑫
Beginner
889 Views

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 !!

0 Kudos
3 Replies
Steven_L_Intel1
Employee
889 Views

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.

0 Kudos
TimP
Honored Contributor III
889 Views

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.

0 Kudos
Steven_L_Intel1
Employee
889 Views

Yes, RECURSIVE or /Qauto (or /Qopenmp) will change this default.

0 Kudos
Reply