在 Fortran 中调用子例程(分段错误)
以下代码在 Linux 系统上使用 pgf90 编译时出现分段错误,而在 Windows 上使用 Intel Visual FORTRAN 时运行成功。
program main
implicit none
integer:: a(3), b(3) ,c(3)
a=[3, 4, 5]
b=[1, 2, 3]
call sub(a,b,c)
write(*,*)'a+b = ',c
end program main
subroutine sub(a,b,c)
implicit none
integer, intent(in)::a(:),b(:)
integer, intent(out)::c(:)
c=a+b
end subroutine sub
对此有何解释?
The following code gives segmentation error when compiled with pgf90 on the Linux system, while is run successfully when I used the Intel Visual FORTRAN on Windows.
program main
implicit none
integer:: a(3), b(3) ,c(3)
a=[3, 4, 5]
b=[1, 2, 3]
call sub(a,b,c)
write(*,*)'a+b = ',c
end program main
subroutine sub(a,b,c)
implicit none
integer, intent(in)::a(:),b(:)
integer, intent(out)::c(:)
c=a+b
end subroutine sub
Any explanation for this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用具有虚拟参数形状的子例程(如本程序中的情况)时,需要显式接口。实现这一点的最简单方法是将子程序放入模块中,并在主程序中使用该模块。
When you call a subroutine which has assumed shape dummy arguments (as is the case in this program), an explicit interface is required. The easiest way to achieve this, is to put the subroutine in a module, and use the module in the main program.
使用标准 Fortran 90 语法可能会有所帮助,特别是在声明和初始化数组的方式方面。
It might be helpful to use standard Fortran 90 syntax, specifically in how you declare and initialize arrays.