Fortran可分配数组的生存期 [英] Fortran allocatable array lifetime

查看:183
本文介绍了Fortran可分配数组的生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码:

 程序测试
调用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆