Fortran最终例程在变量超出范围之前自行调用 [英] Fortran final routine calls itself before variable goes out of scope

查看:142
本文介绍了Fortran最终例程在变量超出范围之前自行调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fortran析构函数/最终例程有问题。
具有以下代码:

  module my_type_module 
隐式无

类型my_type
包含
final ::析构函数
结束类型my_type

接口my_type
!构造函数声明
模块过程my_type_constructor
结束接口my_type

包含
函数my_type_constructor()
类型(my_type):: my_type_constructor

print *,'构造函数地址是:',
& loc(my_type_constructor)

结束函数my_type_constructor

子程序析构函数(this)
类型(my_type):: this
print *,'Des_type_my_type具有地址的对象:',
& loc(this)
结束子程序析构函数

结束模块my_type_module

程序试用

使用my_type_module
隐式无

type(my_type):: argument
$ b print *,'试用程序启动'
print *,'初始地址是',loc(参数)

argument = my_type_constructor()

print *,'Address afer constructor is called is',loc(argument)

print *,'doing some work .. '
print *,'整理程序...'

print *,'最终地址是',loc(参数)

结束程序试用

输出为:

 试用程序启动
初始地址为4351590240
构造函数地址为:140734743834256
my_type对象的析构函数地址为:4351590240
地址为140734743834256的my_type对象的析构函数
构造函数的地址称为i s 4351590240
做一些工作...
完成程序...
最终地址是4351590240

所以看起来构造的对象一旦构造结束而不是在程序结束时就会被破坏。
任何想法有什么不对?
上面的代码是使用ifort 14.0.0编译的, 解决方案

没有错



第一个析构函数调用是完成赋值语句的左侧。



第二个调用是完成函数结果(概念上在该结果的值已用于表达式中并传递到赋值的左侧)。



实体在程序终止前立即生效(这里通过执行结束程序语句)尚未最终确定。


I have a problem with Fortran destructors/final routines. Having the code below:

  module my_type_module
    implicit none

    type my_type
      contains
        final :: destructor
    end type my_type

    interface my_type
      ! Constructor declaration
      module procedure my_type_constructor
    end interface my_type

    contains
      function my_type_constructor()
        type(my_type) :: my_type_constructor

        print *, 'In constructor address is: ',
 &          loc(my_type_constructor)

      end function my_type_constructor

      subroutine destructor(this)
        type(my_type) :: this
        print *, 'Destructor of my_type object with address: ',
 &        loc(this)
      end subroutine destructor

  end module my_type_module

  program trial

    use my_type_module
    implicit none

    type(my_type) :: argument

    print *, 'Trial program starts'
    print *, 'Initial address is', loc(argument)

    argument = my_type_constructor()

    print *, 'Address afer constructor is called is', loc(argument)

    print *, 'doing some work...'
    print *, 'finishing program...'

    print *, 'Final address is', loc(argument)

  end program trial

the output is:

Trial program starts
Initial address is            4351590240
In constructor address is:        140734743834256
Destructor of my_type object with address: 4351590240
Destructor of my_type object with address: 140734743834256
Address afer constructor is called is      4351590240     
doing some work...
finishing program...
Final address is            4351590240

So it seems that constructed object is destructed as soon as its construction ends rather than at the end of the program. Any ideas what's wrong? Above code was compiled with ifort 14.0.0,

解决方案

Nothing is wrong (bar apparent (?) use of fixed form source continuation with Fortran 2003 code in 2013).

The first "destructor" call is to finalize the left hand side of the assignment statement.

The second call is to finalize the function result (conceptually after the value of that result has been used in the expression and transferred to the left hand side of the assignment).

Entities existing immediately prior to termination of the program (here by execution of the end program statement) are not finalized.

这篇关于Fortran最终例程在变量超出范围之前自行调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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