计划接收信号SIGSEGV:段错误 - 无效的内存引用

问题描述:

program height_sand 
     implicit none 
    integer tmax, nmax 
    real zmax, maxx 
    parameter (nmax=200000) 
    real x1(nmax),y1(nmax),z1(nmax),teta 
     !real d, zmax, xmax 
    integer t,ntot 
    integer i, j, id, tip(nmax) 
    open(unit=1,file='vmdfile.xyz') 
    open(unit=3,file='height.dat') 
    ntot=3000 
     tmax=51 
     maxx=0 
     zmax=0 

     do t=1,tmax 
      read(1,*) 
      read(1,*) 


     do i=1,ntot 
     read(1,*)tip(id),x1(id),y1(id),z1(id) 

      if (z1(id).gt.zmax) then 
      zmax=z1(id) 

     end if 

     if (x1(id).gt.maxx) then 
      maxx=x1(id) 
     end if 

      end do 

      teta=zmax/maxx 
      write(3,*)t,zmax,teta 
    end do 


     !110 format(I8, f15.6) 
    END 
+2

编译所有警告和调试信息('gfortran -g-Wall')。使用调试器('gdb')。如果可能的话,切换到最新版本的Fortran(至少Fortran 2003) – 2014-10-01 07:10:45

+0

这不是一个简单的例子,因为没有输入文件,我们不能重现问题。 – Peter 2014-10-01 07:38:56

+2

编译运行时间下标检查。用gfortran,'-fcheck = all'或'-fcheck = bounds'。这会导致编译器查找由高性能标记标识的问题。 – 2014-10-01 15:57:34

此行

  read(1,*)tip(id),x1(id),y1(id),z1(id) 

可能是您报告错误的根源。在执行时id还没有被赋予一个值。如果你认为它会被自动设置为0,那么(a)你认为是错误的,并且(b)tip(0)无论如何都是无效的数组元素引用,因为Fortran索引从1开始(除非你专门将它们设置为另一个起始值,显示的代码不)。

由于循环控制变量(tmaxntot)不在循环内部使用,我强烈怀疑您向我们展示了一个半熟的代码。