Fortran:模块中的可分配变量 [英] Fortran: Allocatable variables in modules

查看:101
本文介绍了Fortran:模块中的可分配变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用数组Ar(-3:3,5),它是模块全局中的可分配变量,并在一个子例程中分配它,并在下一个子例程中访问它(请参见下面的代码段).第二个子例程中的索引是从-3到3,还是从1到5,还是必须在子例程中指定?

I want to use an array Ar(-3:3, 5), which is an allocatable variable in the module global and allocate it in one subroutine and access it in the next subroutine (see code snippets below). Will the indexing in the second subroutine be from -3 to 3 and from 1 to 5, or do I have to specify that in the subroutine?

module global
   real, allocatable(:,:) :: Ar
end module global

subroutine allocateAr
   use global

   ALLOCATE(Ar(-3:3, 5))
end subroutine allocateAr

subroutine useAr
   use global

   Ar(-3,1)=3.0  !is this -3,1 here or do I have to use 1,1????
end subroutine useAr

推荐答案

可分配数组始终保留其边界,如果您以可分配数组的形式访问它们.这意味着即使直接使用使用关联"或主机关联"(如子例程useAR中所示),也可以将它们作为可分配的伪参数传递给它们.如果将它们作为假定的形状或假定的大小数组传递,则必须在每个调用的过程中指定下限,否则它将默认为1.

Allocatable arrays always retain their bounds, if you access them as allocatables. This means even directly using 'use association' or 'host association', as you show in subroutine useAR, or if you pass them as allocatable dummy arguments. If you pass them as assumed shape or assumed size arrays, you must specify the lower bounds in every called procedure, otherwise it will default to 1.

因此,您可以使用-3,1.

否则,我同意乔纳森·杜尔西(Jonathan Dursi)关于全球易变国家的问题.

Otherwise I agree with Jonathan Dursi regarding the global mutable state.

这篇关于Fortran:模块中的可分配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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