以下代码,结合模块程序和外部程序:
module module_dummy
implicit none
contains
subroutine foo(a)
real, intent(inout) :: a(:)
call bar(a)
end subroutine foo
end module module_dummy
program main
use module_dummy
implicit none
integer, parameter :: nelems = 100000000
real, allocatable :: a(:)
allocate( a(nelems) )
a = 0.0
call foo(a)
print *, a(1:10)
deallocate(a)
end program main
subroutine bar(a)
implicit none
real, intent(inout) :: a(:)
a = 1.0
end subroutine bar
似乎失败了:
- 与
segmentation fault
- 打印一个块
0.000
而不是一个块1.000
在我迄今为止尝试过的任何平台上。该问题与 的隐式接口声明有关bar
,实际上可以通过任何方式添加显式接口来解决该问题,例如使用:
module module_dummy
implicit none
contains
subroutine foo(a)
interface
subroutine bar(x)
real, intent(inout) :: x(:)
end subroutine bar
end interface
real, intent(inout) :: a(:)
call bar(a)
end subroutine foo
end module module_dummy
或bar
在要由module_dummy
.
无论如何,我真的不明白首先是什么错误。我在Fortran 90 标准(第 12.3.2.4 节)中发现的内容是:
从过程接口是隐式的作用域单元引用的过程的类型、类型参数和虚拟参数的形状必须使得实际参数与虚拟参数的特征一致。
在这种情况下,该规则似乎得到了尊重,因为a
它总是被声明为
real, intent(inout) :: a(:)
那么,我在解释使先前代码错误的标准时遗漏了什么?