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

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

问题描述

说我们有这个变量定义

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天全站免登陆