在Fortran中调用子例程(分段错误) [英] Calling a subroutine in Fortran (Segmentation fault)

查看:127
本文介绍了在Fortran中调用子例程(分段错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在Linux系统上使用pgf90进行编译时会产生分段错误,而当我在Windows上使用Intel Visual FORTRAN时,该代码会成功运行.

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 

对此有任何解释吗?

推荐答案

当您调用已假定形状伪参数的子例程(在此程序中就是这种情况)时,需要一个显式接口.实现此目的最简单的方法是将子例程放在模块中,然后在主程序中使用该模块.

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中调用子例程(分段错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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