Fortran 函数:指针作为实际参数,目标作为形式 [英] Fortran function: pointer as actual argument and target as formal

查看:37
本文介绍了Fortran 函数:指针作为实际参数,目标作为形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试破译一个 fortran 代码.它将指向函数的指针作为实际参数传递,而形式参数则是目标.它在主程序中定义并分配了一个 globalDATA 类型的指针,然后调用一个传递该指针的函数:

I am trying to decipher a fortran code. It passes a pointer to a function as an actual argument, and the formal argument is instead a target. It defines and allocates a pointer of type globalDATA in the main program, then it calls a function passing that pointer:

module dataGLOBAL
 type globalDATA
   type (gl_1)      , pointer :: gl1
   type (gd_2)      , pointer :: gd2
   type (gdt_ok)    , pointer :: gdtok
   ...
   ...
 end type globalDATA
end module dataGLOBAL


Program main
....
....
use dataGLOBAL   
...
type(globalDATA),pointer :: GD

allocate(GD)
returnvalue = INIT(GD)
....
....
end

函数内容如下:

integer function INIT(GD) result(returnvalue)
....
....
use dataGLOBAL

type(globalDATA)  , target   :: GD

allocate (GD%gl1)
allocate (GD%gd2)
allocate (GD%gdtok)
....
....
end function INIT

这样做有什么意义?为什么主程序中的指针和目标结构的单个组件都必须分配?谢谢A.

What is the meaning of doing this? And why do both the pointer in the main program and the single components of the target structure have to be allocated? thanks A.

推荐答案

一些事情可能会发挥作用......

A few things may come into play...

  • 当您将指针作为实际参数提供给相应的虚拟参数不具有 POINTER 属性的过程(此处的情况)时,与虚拟参数相关联的东西是实际的目标参数指针.所以在这种情况下,传递的东西是 GD (在主程序中)指向的对象 - 由 allocate 语句分配的东西.(当实际和虚拟参数都具有 POINTER 参数时,则 POINTER 本身被通过" - 您可以更改 POINTER 指向的内容,并且该更改会反映在调用范围中.)

  • When you provide a pointer as an actual argument to a procedure where the corresponding dummy argument does NOT have the POINTER attribute (the case here), the thing that is associated with the dummy argument is the target of the actual argument pointer. So in this case, the thing being passed is the object that GD (in the main program) is pointing to - the thing that was allocated by the allocate statement. (When both the actual and dummy arguments have the POINTER argument, then the POINTER itself is "passed" - you can change what the POINTER points to and that change is reflected back in the calling scope.)

因为函数内部的GD伪参数有target属性,所以函数内部的指针可以指向伪参数.您没有显示此类指针的任何声明,但它们可能在省略的代码中.如果 nothing 曾经指向 GD 虚拟参数(包括在任何可能由 INIT 函数调用的过程中),则 TARGET 属性是多余的,但除了抑制一些优化.

Because the GD dummy argument inside the function has the target attribute, pointers inside the function can be pointed at the dummy argument. You don't show any declarations for such pointers, but perhaps they are in elided code. If nothing is ever pointed at the GD dummy argument (including inside any procedures that might be called by the INIT function), then the TARGET attribute is superfluous, but harmless apart from inhibiting some optimisations.

具有指针属性的事物也(由语言规则自动)具有 TARGET 属性 - 所以主程序中的 GD 具有 TARGET 属性.主程序和函数中的 GD 都具有目标属性的事实可能是相关的,因为...

Things that have the pointer attribute also (automatically by language rules) have the TARGET attribute - so GD in the main program has the TARGET attribute. The fact that GD in the main program and in the function BOTH have the target attribute may be relevant because...

当虚拟参数具有 TARGET 属性并且作为实际参数传递的事物具有 TARGET 属性时,与过程中的虚拟参数相关联的指针也是通常"的(存在异常/处理器依赖项与相应的实际参数相关联的共索引事物/非连续数组/向量下标部分太复杂以至于我记不住).如果指针不是局部变量(可能是在模块中声明的指针),则此关联在过程结束后仍然存在.也许这与省略的代码有关.(或者,如果实际参数没有 TARGET 属性,则与虚拟参数关联的任何指针在过程结束时都将变为未定义.)

When the dummy argument has the TARGET attribute and the thing passed as the actual argument has the TARGET attribute, then pointers associated with the dummy argument inside the procedure are also "usually" (there are exceptions/processor dependencies for coindexed things/non-contiguous arrays/vector subscripted sections too complicated for me to remember) associated with the corresponding actual argument. If a pointer is not a local variable (perhaps it is a pointer declared in a module) then this association survives past the end of the procedure. Perhaps that's relevant in the elided code. (Alternatively, if the the actual argument does not have the TARGET attribute, then any pointers associated with the dummy argument become undefined when the procedure ends.)

globalDATA 类型的组件本身就是指针.因此,主程序中的 GD 是一个指向某物(由主程序中的单个 ALLOCATE 语句分配的东西)的指针,它本身包含指向其他东西的指针(那些其他东西由众多函数中的 ALLOCATE 语句).您有两个级别的指针,因此有两个级别的 ALLOCATE.

The components of the globalDATA type are themselves pointers. Consequently, GD in the main program is a pointer to something (that something being allocated by the single ALLOCATE statement in the main program) that itself contains pointers to other things (those other things being allocated by the numerous ALLOCATE statements in the function). You have two levels of pointer, hence two levels of ALLOCATE.

在 Fortran 2003(或带有可分配 TR"的 Fortran 95)之前,您不能在派生类型中有 ALLOCATABLE 组件,也不能有 ALLOCATABLE 虚拟参数 - 当动态分配的需求与这些冲突时以前的限制,您必须改用指针,即使您只使用指针作为值.我强烈怀疑您的代码可以追溯到这个时代(大约十年前,对可分配 TR 的支持变得普遍).在非常现代"的 Fortran 指针中(应该?)仅在您可能想要指向其他事物的变量时使用(其他事物包括无").

Before Fortran 2003 (or Fortran 95 with the "allocatable TR") you couldn't have ALLOCATABLE components in derived types, and you couldn't have ALLOCATABLE dummy arguments - when the need for dynamic allocation collided with these former restrictions you had to use pointers instead, even if you were only using the pointers as values. I strongly suspect your code dates from this era (Support for the allocatable TR became widespread about a decade ago). In very "modern" Fortran pointers are (should?) only used when you might want variables that point at other things (where other things includes "no thing").

这篇关于Fortran 函数:指针作为实际参数,目标作为形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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