Fortran 第二次用户输入后出现分段错误

Fortran 第二次用户输入后出现分段错误,fortran,Fortran,我编译如下: f77 -c readheader.F f77 -c readheader2.F make (for the main program, which is necessary to have a makefile for) 并运行: ./decpar 输出: Which datafile? Point 2a Program received signal SIGSEGV: Segmentation fault - invalid memory reference. Back

我编译如下:

f77 -c readheader.F
f77 -c readheader2.F
make (for the main program, which is necessary to have a makefile for)
并运行:

./decpar
输出:

Which datafile?
Point 2a

Program received signal SIGSEGV: Segmentation fault - invalid memory
reference.

Backtrace for this error:
#0  0x7FF5E4A62E08
#1  0x7FF5E4A61F90
#2  0x7FF5E41934AF
#3  0x7FF5E4B25C40
#4  0x7FF5E4B26197
#5  0x7FF5E4B34B87
#6  0x4056B8 in __mysubs2_MOD_readheader2
#7  0x401CA5 in MAIN__ at decpar.F:29 (discriminator 88)
Segmentation fault (core dumped)
我不打算发布整个代码,因为它庞大且不必要。幸运的是,错误出现在前几行中。我有两个模块,在这两个模块中,用户都需要键入数据文件的名称,以便提取信息并将其定向到主程序。我试图刷新数据文件,以防由于某种奇怪的原因,第二次用户输入没有覆盖数据文件,我得到了分段错误-无效内存引用。。我试图将datafile的名称更改为datafile2,以便为第二个输入创建新的空间,但遇到的问题与名称相同时的问题相同。我所尝试的一切都在“点2a”停止。另外,如果我切换模块调用的顺序,问题仍然存在于第二个文件中。在多次尝试gfortran-c-g-fbacktrace-fcheck=all之后,我得到的错误是: Fortran运行时错误:数组“thetax”的维度1的索引“1”高于0的上限

注意:一些没有正确缩进的命令就是这样写的,用于格式化

  module MySubs
  contains

  subroutine readheader(theta,thetax,thetay,thetaz)

  include 'psize.inc'
  include 'pinfo.inc'
  #include "rvarrays.inc"
  include 'itype.inc'
  include 'iodefs.inc'

  REAL,ALLOCATABLE, intent(inout), DIMENSION (:) :: thetay, thetaz
  REAL,ALLOCATABLE, intent(inout), DIMENSION (:) :: theta, thetax

  ALLOCATE (theta(nobj),thetax(nobj),thetay(nobj),thetaz(nobj))

  1    print*,'Which datafile?'
  read(*,'(a)') datafile 

  CALL readdata

  do i=1,nobj
    if (i.LT.nobj) then
        thetax(i)=(v(1,i+1)-v(1,i))*(r(1,i+1)-r(1,i))**(-1)
        thetay(i)=(v(2,i+1)-v(2,i))*(r(2,i+1)-r(2,i))**(-1)
        thetaz(i)=(v(3,i+1)-v(3,i))*(r(3,i+1)-r(3,i))**(-1)
        theta(i)=thetax(i)+thetay(i)+thetaz(i)
        else 
        thetax(i)=(v(1,i)-v(1,i-1))*(r(1,i)-r(1,i-1))**(-1)
        thetay(i)=(v(2,i)-v(2,i-1))*(r(2,i)-r(2,i-1))**(-1)
        thetaz(i)=(v(3,i)-v(3,i-1))*(r(3,i)-r(3,i-1))**(-1)
        theta(i)=thetax(i)+thetay(i)+thetaz(i)
    endif 
  end do
  RETURN
  end subroutine readheader

  end module MySubs

我有一点猜测,假设代码是正确的。你有一个C预处理器的电话

#include "rvarrays.inc"
可能其中定义了一些数组。在中没有-cpp标志

include不起作用,并且未定义数组。但是由于没有隐式none,这些数组的名称仍然是有效的名称,但对于标量来说是有效的。尝试添加标志,或删除Fortran include的标志


另一方面,您应该在没有-cpp标志的情况下从中获得编译错误

幸运的是,我自己解决了这个问题,因为根本不可能让你安装代码,因为这是必需的,即使我上传了所有的文件——我不能,因为那是别人的财产。我的数组似乎是分配的,但它们不是;程序当时不知道'nobj'的值。因此,将所有ALLOCATE语句放在调用之后,解决了问题

尝试编译readheader。。。还具有调试选项和边界检查选项。在read语句之后直接写出read语句的结果。试着制作一个小程序,它也会显示同样的问题。欢迎使用,请对所有Fortran问题使用tag。您的代码当然不是Fortran 77,它是一个更新的标准。事实上,您正在使用一个f77命令是完全无关的,它可能只是指向gfortran或其他编译器,请指定。我们需要你的帮助。使用可以使用的所有调试标志编译代码,例如,-g-fbacktrace-fcheck=all。我使用了f77,因为这是安装代码时SYS配置的唯一选项。无论如何,我使用您建议的调试标志再次运行,并得到:Fortran运行时错误:数组“thetax”的维度1的索引“1”高于0的上限。我想我超出了我的堆栈大小,但我不知道如何修复它。您的问题将包含完整的错误消息。这不是堆栈大小,而是编程错误。程序正在访问边界之外的数组。不要忘记,请注意“最小”和“完整”这两个词。@这就是为什么您应该开始准备链接的原因。您真的阅读了链接吗?MCVE不是jsut,上传大量的代码,请看,您确实应该自己努力将问题隔离到一个较小的代码示例中。这并不容易,但却是必要的。如果是gfortran,并且OP sis似乎没有响应我的澄清请求,那么cpp将自动运行任何资本F延期。大多数其他编译器也是如此。如果cpp没有运行,人们会看到非常不同的灾难性错误消息。我们真的应该避免通过这种猜测来回答不完整的问题。啊,我忘记了F。这是一个有趣的挑战,但我知道你是对的@vladimirF这就是为什么vladimirF和我已经写过使用调试标志-fcheck=all,-g等。这可能会给你一个简单的指示,告诉你哪里出了问题。你永远不应该上传所有文件。你应该找出一个最简单的例子,这个例子很容易与问题相吻合。事实上,一个人经常在孤立他人的过程中发现错误。我仍然相信你甚至懒得阅读链接。@albert我使用了它们,只是错误消息仍然与我前面说过的两次相同Wel你解决了问题,所以一切正常,但奇怪的是,即使使用了建议的调试标志,错误消息仍然是一样的,我希望至少有一次错误用行号绑定消息..@albert确实有一条错误消息,它在注释中:Fortran运行时错误:数组“thetax”的维度1的索引“1”高于上限0。我一再要求把这一点和问题的完整背景放在一起,但被忽略了。
  PROGRAM decpar

  use MySubs
  use MySubs2

  include 'psize.inc'
  include 'pinfo.inc'
  #include "rvarrays.inc"
  include 'itype.inc'
  include 'iodefs.inc'

  REAL,ALLOCATABLE,DIMENSION(:) :: dztheta,dthetax,dthetay,dthetaz
  REAL,ALLOCATABLE,DIMENSION(:) :: convtheta,dttheta,q1,q2,q3,q
  REAL,ALLOCATABLE,DIMENSION(:) :: theta,thetax,thetay,thetaz
  REAL,ALLOCATABLE,DIMENSION(:) :: thetapx, thetapy, thetapz,thetap
  REAL,ALLOCATABLE,DIMENSION(:) :: dthetasp

  ALLOCATE(dztheta(nobj),dthetax(nobj),dthetay(nobj),dthetaz(nobj))
  ALLOCATE(q(nobj),dthetasp(nobj),q3(nobj))
  ALLOCATE(convtheta(nobj),dttheta(nobj),q1(nobj),q2(nobj))

  CALL readheader(theta,thetax,thetay,thetaz)
  CALL readheader2(thetapx, thetapy, thetapz,thetap)

  q0=-0.55
  do i=1,nobj
    dztheta(i)=(thetap(i)-theta(i))*(z1-z2)**(-1)
    dttheta(i)=-(2./3.)*dztheta(i)
    if (i<nobj) then
       dthetax(i)=r(1,i)*(thetax(i+1)-thetax(i))/(r(1,i+1)-r(1,i))
       dthetay(i)=r(2,i)*(thetay(i+1)-thetay(i))/(r(2,i+1)-r(2,i))
       dthetaz(i)=r(3,i)*(thetaz(i+1)-thetaz(i))/(r(3,i+1)-r(3,i))
       dthetasp(i)=dthetax(i)+dthetay(i)+dthetaz(i)
    else 
       dthetax(i)=r(1,i)*(thetax(i)-thetax(i-1))/(r(1,i)-r(1,i-1))
       dthetay(i)=r(2,i)*(thetay(i)-thetay(i-1))/(r(2,i)-r(2,i-1))
       dthetaz(i)=r(3,i)*(thetaz(i)-thetaz(i-1))/(r(3,i)-r(3,i-1))
       dthetasp(i)=dthetax(i)+dthetay(i)+dthetaz(i)
    endif
  convtheta(i)=dttheta(i)+h100*100.0*dthetasp(i)  
  q1(i)=-1+(1+q0)*(1+theta(i)/(3.0*100.0*h100))**(-2)
  q2(i)=-(dttheta(i)/(3*100*h100)**2)
  q3(i)=(1+theta(i)/(3*100*h100))**(-2)
  q(i)=q1(i)+q2(i)*q3(i)
  end do

  DEALLOCATE(dztheta,dthetax,dthetay,dthetaz)
  DEALLOCATE(q,q1,q2,q3,dthetasp)
  DEALLOCATE(convtheta,dttheta)

  end
#include "rvarrays.inc"
f77 -c readheader.F
f77 -c readheader2.F