检查fortran重载分配中的自我分配 [英] checking for self-assignment in fortran overloaded assignment

查看:71
本文介绍了检查fortran重载分配中的自我分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用fortran 2003实现一个带有重载算术运算和赋值的多项式类.派生类型会维护可分配的术语定义和系数列表,像这样

I am trying to implement a polynomial class with fortran 2003, with overloaded arithmetic operations and assignments. The derived type maintains allocatable list of term definitions and coefficients, like this

type polynomial
  private
    type(monomial),dimension(:),allocatable     ::  term
    double precision,dimension(:),allocatable   ::  coef
    integer                                     ::  nterms=0
  contains
   ...
end type polynomial

interface assignment(=)
   module procedure :: polynomial_assignment
end interface
   ...
contains
    elemental subroutine polyn_assignment(lhs,rhs)
      implicit none
      type(polynomial),intent(???)  :: lhs
      type(polynomial),intent(in)   :: rhs
 ...

我必须使它成为元素,因为它打算用作多项式的矩阵.这确实有效,至少在大多数情况下如此.但是,我不知何故让自己对这里的自我分配感到担忧.可以简单地检查指针以查看C ++中的内容是否相同,但是在Fortran中似乎不是一种选择.但是,编译器确实会检测到自赋值,并给了我一个警告.(gfortran 4.9.0)

I had to make it elemental because this is intended to be used as matrices of polynomials. That does work, for the most cases at least. However, I somehow got myself into concerns about self-assignment here. One can simply check the pointers to see if things are the same in C++, but it doesn't seem to be an option in Fortran. However the compiler do detect the self-assignment and gave me a warning. (gfortran 4.9.0)

当我有lhs的 intent(out)时,lhs和rhs的可分配条目似乎在子例程的入口处被重新分配,这很有意义,因为它们都是p,并且是一个意图(out)参数将首先确定.

When I have intent(out) for lhs, the allocatable entries for both lhs and rhs appeared to be deallocated on entry to the subroutine, which made sense since they were both p, and an intent(out) argument would first be finalized.

然后我尝试避免使用intent(inout)进行释放,并通过修改lhs输出中的一个字段来检查自赋值

Then I tried to avoid the deallocation with an intent(inout), and check self-assignment by modifying one field in the lhs output

   elemental subroutine polyn_assignment(lhs,rhs)
      implicit none
      type(polynomial),intent(inout)  :: lhs
      type(polynomial),intent(in)   :: rhs
      lhs%nterms=rhs%nterms-5
      if(lhs%nterms==rhs%nterms)then
        lhs%nterms=rhs%nterms+5
        return
      end if
      lhs%nterms=rhs%nterms

好吧,这就是令我惊讶的地方.当我这样做

Well, now this is what surprised me. When i do

p=p

它没有进行测试,而是继续进行,给了我多项式为0的多项式,但没有违反内存.感到困惑,我在作业中打印了lhs%nterms和rhs%nterms,却发现它们是不同的!

It didn't make the test and proceeded, giving me a polynomial with 0 terms but no memory violations. Confused, I printed lhs%nterms and rhs%nterms inside the assignment, only to find that they are different!

更令人困惑的是,当我用相同的东西做

What is even more confusing is that when I did the same thing with

call polyn_assignment(p,p)

它运行完美,并且检测到两个参数都相同.我不知道子例程的接口如何与子例程本身不同地运行.

It works perfectly and detected that both arguments are the same. I am puzzled how an interface of a subroutine can run differently from the subroutine itself.

我错过了有关Fortran 2003作业的特别之处吗?

Is there something special about assignment in Fortran 2003 that I've missed?

(第一次在这里问一个问题.如果我做错了,请纠正我.)

(First time to ask a question here. Please correct me if i didn't do it right.)

推荐答案

如果您有一条语句 a = b 通过子例程 sub 调用已定义的赋值,则该赋值语句等效于 call sub(a,(b)).注意括号-右侧参数是对带括号的表达式求值的结果,因此在概念上与 b 不是同一对象.有关详细信息,请参见F2008 12.4.3.4.3.

If you have a statement a = b that invokes defined assignment via a subroutine sub, the assignment statement is equivalent to call sub(a, (b)). Note the parentheses - the right hand side argument is the result of evaluating a parenthesised expression and is therefore not conceptually the same object as b. See F2008 12.4.3.4.3 for details.

因此, a = a 等同于 call sub(a,(a)).这两个参数没有别名.它与 call sub(a,a)不同,后者可能(取决于 sub 的内部细节,包括虚拟参数属性)违反了Fortran的参数别名规则.(例如,在您的示例中,诸如 call polyn_subroutine(a,a)之类的语句是非法的.)

Consequently, a = a is equivalent to call sub(a, (a)). The two arguments are not aliased. It is different from call sub(a,a), the latter may (depending on the specifics of the internals of sub, including dummy argument attributes) break Fortran's argument aliasing rules (e.g. in your example, a statement such as call polyn_subroutine(a,a) is illegal).

这篇关于检查fortran重载分配中的自我分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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