将可分配变量传递给具有不可分配参数的子例程有什么影响? [英] What is the effect of passing an allocatable variable into a subroutine with non-allocatable argument?

查看:10
本文介绍了将可分配变量传递给具有不可分配参数的子例程有什么影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有这个变量定义

Say we have this variable definition

Real*8, Dimension(:), Allocatable :: dblA
Allocate  (dblA(1000))

现在我调用这个子程序:

Now I call this subroutine:

Call MySub(dblA)

其中:

Subroutine MySub(dblA)
Real*8,  INTENT(Out), DIMENSION(1000) :: dblA
End

这种做法的副作用是什么?我应该避免这种情况吗?

What is the side effect of such practice? Should I avoid this?

推荐答案

High Performance Mark 的回答 说出了我想要的一切.不过,为了强调,我将重复一个方面.

High Performance Mark's answer says about all I wanted. I will repeat one aspect though, for emphasis.

在调用子程序时非常需要分配子程序中的可分配数组.[并且在问题的情况下,足够大.]

It is very much required that the allocatable array in the subroutine be allocated when the subroutine is called. [And in the case of the question, of sufficient size.]

为了使这个答案不仅仅是评论,我将解决更一般的问题标题.在此之前,我将看看围绕可分配或不可分配的虚拟参数"的思考中可能出现的问题.

To make this answer more than just a comment I'll address the more general question title. Before that, I'll look at things which could feature in the thinking around "allocatable or non-allocatable dummy argument".

  • 因为虚拟参数(子程序中的那个)具有 intent(out) 属性,所以实际参数变得未定义.如果虚拟参数是可分配的,它也会在进入时被释放.在这种情况下,实际参数保持分配状态并保持其原始大小.

  • Because the dummy argument (the one in the subroutine) has the intent(out) attribute it, and the actual argument, becomes undefined. If the dummy argument is allocatable it is also deallocated on entry. In this case, the actual argument remains allocated and of its original size.

如果不是可分配的,虚拟参数(和实际参数)不能查询或更改分配状态;虚拟参数不能进一步传递给期望可分配参数的过程,即使实际参数可能是.

Without being allocatable, the dummy argument (and the actual argument) cannot have allocation status queried or changed; the dummy argument cannot further be passed to a procedure expecting an allocatable argument even though the actual argument could be.

如果虚拟参数是可分配的,而不是(静态)显式形状数组,则实际参数也必须是可分配数组.

If the dummy argument were allocatable instead of being a (static) explicit shape array, the actual argument must also be an allocatable array.

使用可分配的虚拟参数时,在引用子例程时将需要显式接口.[即使在隐式接口很好的情况下,也有人会说这是一个好主意.]

With an allocatable dummy argument an explicit interface would be required when the subroutine is referenced. [Some would say this is a good idea even in the cases where an implicit interface is fine.]

谈到更一般的事情,我将举一个例子,其中对可分配的实际/不可分配的虚拟对象有限制(F2008,12.5.2.4):

Coming to the more general thing, I'll give an example where there is a restriction on allocatable actual/non-allocatable dummy (F2008, 12.5.2.4):

如果实际参数是多态共索引对象,则虚拟参数不应是多态的.

If the actual argument is a polymorphic coindexed object, the dummy argument shall not be polymorphic.

但这并不是大多数人需要担心的问题.

But this isn't something to worry about for most people.

哦,还有一件事我要从另一个答案中强调:Real*8 应该避免.

Oh, and another thing I'll restress from that other answer: Real*8 should be avoided.

这篇关于将可分配变量传递给具有不可分配参数的子例程有什么影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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