what is the difference of the local variables in subroutine and function

what is the difference of the local variables in subroutine and function

Sometimes i found the local variables in subroutine was not released after it was executed. How Fortran deal with the local variable in subroutine? Is the local variable in function released as soon as the function was executed?

fortran
9 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

For SAVE variables, the storage must be retained.  Variables not so declared are available to be over-written after return from function, but there's no guarantee.

Quote:

TimP (Intel) wrote:

For SAVE variables, the storage must be retained.  Variables not so declared are available to be over-written after return from function, but there's no guarantee.

Do you means that after return from the function some variables may can not be over-written? So if in the following function the variable with the same name of a variable in the former function may can not be changed?

fortran

The local non-SAVEd variables used in a function aren't available after return from the function, when they go out of scope, regardless of whether they happen to have been over-written.  We would really need an example to clarify your intent.

It is necessary to use clear and precise language to describe the topic of this thread. A symbolic debugger attempts to give a logically correct picture of the status of local variables, but it cannot do so if the code is optimized or the program is multi-threaded and the debugger is unaware of that fact. You should not draw strong conclusions from what you see in the debugger.

"Do you means that after return from the function some variables may can not be over-written?" is an improper question. Unsaved local variables have no existence for a subprogram that has returned and is, therefore, inactive. In an optimized program with no debugging information, symbolic names of local variables do not even exist.

The same local variable in a given subprogram may occupy different memory locations on the stack from one activation to the next. Those memory locations could have been used for local variables in other subprograms, and their value is undefined at the point of entry to the first subprogram.

This problem was solved by change the complier.  So i do not have an example now. if i come across it again, i will upload it to you. 

i did not mean declear once in a subroutine and use many times in others. Within each subroutine i declared the same variables. When calling the first subroutine everything is ok, but when calling the subsequent one, it did not get the proper value.

Maybe  it is due to the mistake of installing the compiler. but i am not very sure.

fortran

>>i did not mean declear once in a subroutine and use many times in others. Within each subroutine i declared the same variables. When calling the first subroutine everything is ok, but when calling the subsequent one, it did not get the proper value.

Your statement implies to us readers that the variables you declare and use in one subroutine are intended to be used in a different subroutine without being passed as arguments between the two subroutines. For this to occur, the shared variables must be either a) in a COMMON block (preferrably named), or b) in a module as variables contained therein. The preference is for you to use b) (modules).

Jim Dempsey

www.quickthreadprogramming.com

Quote:

jimdempseyatthecove wrote:

>>i did not mean declear once in a subroutine and use many times in others. Within each subroutine i declared the same variables. When calling the first subroutine everything is ok, but when calling the subsequent one, it did not get the proper value.

Your statement implies to us readers that the variables you declare and use in one subroutine are intended to be used in a different subroutine without being passed as arguments between the two subroutines. For this to occur, the shared variables must be either a) in a COMMON block (preferrably named), or b) in a module as variables contained therein. The preference is for you to use b) (modules).

Jim Dempsey

I use module to share variables between subroutines. But the value of some local variables declared inside one subroutine was retained to another one, and can not be changed as i want.

fortran

In the sketch code you posted in the other thread (please read my comment in the other thread too), upon the exit of the DO i=, i will contain the last value used (or first value of i= if iteration count .le. 0). When i not registered by optimization, the stack location reserved for i will be written. After return from b1, should you immediately call b2, and before you enter the DO loop, the value at the former memory location of i in b1 coincidently is also the same memory location of i in b2, AND it is uninitialized (but holding the last value of i on exit from b1). Had b2 declared INTEGER k,j,i, then k would have aligned with former i of b1. Also, after call to b1, had you called something else (example WRITE(*,*) "Boink") Then the call would likely have overwritten the memory location that had held i in the call to b1, and would now appear as something else.

Jim Dempsey

www.quickthreadprogramming.com

Kommentar hinterlassen

Bitte anmelden, um einen Kommentar hinzuzufügen. Sie sind noch nicht Mitglied? Jetzt teilnehmen