Fortran可分配数组的生存期 [英] Fortran allocatable array lifetime
问题描述
程序测试
调用foo
调用foo
包含
子程序foo
整数(8),维(:),allocatable :: var1
分配(var1(10))
...
返回
结束子程序foo
结束
变量var1会被分配两次吗? (我猜是)。如果它被分配给每个调用,第一次调用期间分配的内存是否变为空闲?
每次执行ALLOCATE语句时(即每次调用foo过程时)都会被分配。在Fortran 90(仅)的规则下,当过程结束时,foo的分配状态变得未定义。一个具有未定义分配状态的变量将变得不可用 - 在随后的过程调用中您不能合法地重新分配。
在Fortran 95及更高版本中,因为它是本地,非保存的变量,var1将在每次执行foo过程结束时解除分配。
Say I have the below code:
program test
call foo
call foo
contains
subroutine foo
integer(8),dimension(:),allocatable:: var1
allocate(var1(10))
...
return
end subroutine foo
end
will the variable var1 get allocated twice? (I guess YES). If it is allocated for each call, will the memory allocated during the first call becomes free?
var1 will (attempt to) be allocated every time the ALLOCATE statement is executed (i.e. every time the foo procedure is called).
Under the rules of Fortran 90 (only) the allocation status of foo becomes undefined when the procedure ends. A variable with undefined allocation status is rendered unusable - you cannot legally re-allocate in a subsequent call of the procedure.
In Fortran 95 and later, because it is a local, non-saved variable, var1 will be deallocated every time execution of the foo procedure ends.
这篇关于Fortran可分配数组的生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!