终结例程是否需要元素才能在超出范围的可分配数组的元素上调用? [英] Does the finalization routine need to be elemental in order to be called on the elements of allocatable array that goes out of scope?

查看:10
本文介绍了终结例程是否需要元素才能在超出范围的可分配数组的元素上调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个可分配的可终结派生类型的数组,当数组超出范围时,是否会在每个单独的元素上调用终结器?

If I have an allocatable array of a finalizable derived type, will the finalizer be called on every individual element when the array goes out of scope?

这是一个说明问题的小代码示例:

Here is a small code example that illustrates the question:

module LeakyTypeModule

   implicit none
   private

   type, public :: LeakyType
      real, pointer :: dontLeakMe(:) => null()
   contains
      procedure :: New
      final     :: Finalizer
   end type

contains

   subroutine New(self, n)
      class(LeakyType), intent(out) :: self
      integer         , intent(in)  :: n
      allocate(self%dontLeakMe(n))
      self%dontLeakMe = 42.0
   end subroutine

   subroutine Finalizer(self)
      type(LeakyType), intent(inout) :: self
      if (associated(self%dontLeakMe)) deallocate(self%dontLeakMe)
   end subroutine

end module


program leak

   use LeakyTypeModule
   implicit none

   type(LeakyType), allocatable :: arr(:)

   allocate(arr(1))
   call arr(1)%New(1000)
   deallocate(arr)

end program

请注意,此程序泄漏了 LeakyTypeNew() 方法中分配的 dontLeakMe 数组.起初这对我来说有点令人惊讶,但后来我发现可以通过声明终结器 elemental 来解决这个问题.gfortran 和 ifort 的行为方式相同,因此我假设此行为遵循 Fortran 2003 标准.

Note that this program leaks the dontLeakMe array allocated in the New() method of LeakyType. At first this was a bit surprising for me but, then I discovered that the problem can be fixed by declaring the finalizer elemental. Both gfortran and ifort behave in the same way, so I assume this behaviour is following the Fortran 2003 standard.

谁能证实这一点?老实说,我很难理解标准在这一点上的含义.

Can anyone confirm this? To be honest I have a hard time time understanding what the standard says on this particular point.

现在我也看不到在 not 中声明我所有的终结器元素有多大用处.这是否有我忽略的应用程序?

Right now I also can't see much use in not declaring all my finalizers elemental. Does this have any application I'm overlooking?

推荐答案

判断是否调用final过程,调用哪个final过程的规则,在排序匹配要求方面与通用过程的解析相同.

The rules for determining whether a final procedure is invoked, and which final procedure is invoked, are the same as for resolution of generic procedures in terms of rank matching requirements.

注意问题被标记为 Fortran 2003...

Noting that the question is tagged Fortran 2003...

Fortran 2003 及更早版本中的基本过程必须是 PURE.如果你的终结器需要做一些与纯属性不兼容的事情(这是相当常见的),那么终结器不能是基本的,你需要编写特定于等级的变体.

Elemental procedures in Fortran 2003 and prior have to be PURE. If your finalizer needs to do something that is incompatible with the pure attribute (which is reasonably common) then the finalizer cannot be elemental, and you need to write the rank specific variants.

Fortran 2008 引入了 IMPURE ELEMENTAL 的概念,这对于编写终结器非常方便.

Fortran 2008 introduces the concept of IMPURE ELEMENTAL, which is quite handy for writing finalizers.

这篇关于终结例程是否需要元素才能在超出范围的可分配数组的元素上调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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