将具有TARGET属性的参数提供给具有TARGET属性的伪参数的过程 [英] Providing an argument that has not the TARGET attribute to a procedure with a dummy argument that has the TARGET attribute
问题描述
在Fortran语言中,如果将具有TARGET属性的参数提供给具有TARGET属性的伪参数的过程,则应导致无效代码。但是,当使用gfortran(5.1.0)或ifort(14.0.0)编译以下代码时,不会检测到错误,并且程序的行为与参数实际上具有TARGET属性相同。我错了,当我说它是一个无效的代码,或者这是一个编译器缺陷?
程序指示
隐含无
整数,维(3):: A
整数,维(:),指针:: ptr_A
A = [1,2,3]
调用指针大小(A,ptr_A)
print *,A =,ptr_A
包含
子程序指针大小(tab,ptr_tab)
整数, (:),intent(in),target :: tab
integer,dimension(:),pointer :: ptr_tab
ptr_tab =>标签
结束子程序
结束程序
<你是正确的,你有无效的代码。这不是,因为你说的原因。在你的情况下,假参数 tab
具有目标属性是合法的,即使相关的实际参数 A
还没有。
即使是指针赋值语句 ptr_tab => tab
是合法的。
然而,重要的是Fortran 2008中的以下内容(C.9.4,另请参阅12.5.2.4)
如果一个非伪指针伪参数具有TARGET属性并且相应的实参不包含任何与伪参数关联的指针,并且因此在程序的
执行过程中,实际的参数在程序执行完成时变成未定义。
也就是说,完成指针化
ptr_A
不再是定义的关联状态。
感兴趣的话,使用nagfor编译结果
运行时错误:aaa.f90,第9行:引用悬挂指针PTR_A
目标已从过程返回POINTERIZATION:POINTERIZE
程序由致命错误终止
Abort核心转储)
但同样,仅仅因为指针关联未定义,这并不意味着你无法获得您期望的结果。这样的检查对于编译器来说是一件好事,但它不一定要失败。
In Fortran language, providing an argument that has not the TARGET attribute to a procedure with a dummy argument that has the TARGET attribute should lead to an invalid code. However, when the following code is compiled with gfortran (5.1.0) or ifort (14.0.0), no error is detected and the program behaves like the argument actually has the TARGET attribute. Am I wrong when I say it is an invalid code or is this a compiler defect?
program pointerization
implicit none
integer, dimension(3) :: A
integer, dimension(:), pointer :: ptr_A
A = [1, 2, 3]
call pointerize(A, ptr_A)
print*, "A=", ptr_A
contains
subroutine pointerize(tab, ptr_tab)
integer, dimension(:), intent(in), target :: tab
integer, dimension(:), pointer :: ptr_tab
ptr_tab => tab
end subroutine
end program
You are correct that you have invalid code. It isn't, though, for the reason you say. It is legal, in your case, for the dummy argument tab
to have the target attribute even though the associated actual argument A
hasn't.
Even the pointer assignment statement ptr_tab => tab
is legal.
What is important, however, is the following from Fortran 2008 (C.9.4, see also 12.5.2.4)
If a nonpointer dummy argument has the TARGET attribute and the corresponding actual argument does not, any pointers that become associated with the dummy argument, and therefore with the actual argument, during execution of the procedure, become undefined when execution of the procedure completes.
That is, on completion of pointerize
ptr_A
is no longer of defined association status. Deferencing this pointer in your print statement is not allowed.
For interest, compiling with nagfor results in
Runtime Error: aaa.f90, line 9: Reference to dangling pointer PTR_A
Target was RETURNed from procedure POINTERIZATION:POINTERIZE
Program terminated by fatal error
Abort (core dumped)
But equally, just because the pointer association is undefined, this doesn't mean that you can't get the results you expect. Such checking is a nice thing from a compiler but it isn't required that it should fail.
这篇关于将具有TARGET属性的参数提供给具有TARGET属性的伪参数的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!