在 Fortran 中调用子例程(分段错误)

发布于 2025-01-08 14:23:39 字数 383 浏览 6 评论 0原文

以下代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2025-01-15 14:23:39

当您调用具有虚拟参数形状的子例程(如本程序中的情况)时,需要显式接口。实现这一点的最简单方法是将子程序放入模块中,并在主程序中使用该模块。

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.

青衫儰鉨ミ守葔 2025-01-15 14:23:39

使用标准 Fortran 90 语法可能会有所帮助,特别是在声明和初始化数组的方式方面。

 program main 
 implicit none 
 integer, dimension(3):: a, b ,c
 a=(/3, 4, 5/)
 b=(/1, 2, 3 /)
 call sub(a,b,c)
 write(*,*)'a+b = ',c 
 end program main

It might be helpful to use standard Fortran 90 syntax, specifically in how you declare and initialize arrays.

 program main 
 implicit none 
 integer, dimension(3):: a, b ,c
 a=(/3, 4, 5/)
 b=(/1, 2, 3 /)
 call sub(a,b,c)
 write(*,*)'a+b = ',c 
 end program main
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文