linux - forrtl : severe (174): SIGSEGV, 发生段错误

标签 linux fortran

我真的需要你的帮助!我想知道是否是因为数组坐标的原因。

我使用 ifort msd.f90 -o msd.x 编译了一个 Fortran 程序

在我运行 ./msd.x 后,它给了我段错误

我的 Fortrain 代码是:

program mean_square_displacement
  implicit none
  integer i,j,k,natom,mstep
  integer, parameter :: mmax=5000,nmax=1000,kmax=3 
  real*16 vector(3,3),d(3)
  real*16,dimension(kmax,nmax,mmax) :: coor
  real*16 msdtotal(mmax),msd(nmax,mmax)


  open(unit=8,file="vector")
  read(unit=8,fmt=*) (vector(k,1),k=1,3)
  read(unit=8,fmt=*) (vector(k,2),k=1,3)
  read(unit=8,fmt=*) (vector(k,3),k=1,3)


  do j=0,mmax
  do i=0,nmax
  do k=0,kmax
  coor(k,i,j)=0.d0  
  enddo
  enddo
  enddo



 open(unit=9,file="trace")

  i=0
  j=0
  10    continue
  read(unit=9,fmt=*,end=100) (d(k),k=1,3)
  i=i+1

     coor(1,i,j)=d(1)
     coor(2,i,j)=d(2)
     coor(3,i,j)=d(3)

    write(6,20), coor(1,i,j),coor(2,i,j),coor(3,i,j)

  goto 10
  100   continue

  natom=130
  mstep=j


  20    format(3(1x,f12.9))

  stop
  end 

最佳答案

段错误通常是由于访问数组超出其分配的边界而引起的。

在您的情况下,数组coor 被分配为coor(1:kmax,1:nmax,1:nmax),其中1 是默认下界。但是,您经常访问一个或多个索引为 0 的数组。

如果您想更改下限,可以使用dimension(0:kmax,0:nmax,0:nmax)分配数组。

<小时/>

顺便说一句:您的读取循环不会增加 j 并且您应该避免使用 goto,因为它的样式很糟糕且难以阅读。

可以使用IOSTAT=说明符检测文件结尾。您的读取循环将如下所示:

i=0
j=0
do
    read(unit=9,fmt=*,iostat=ios) (d(k),k=1,3)
    if (iostat /= 0) exit ! exit the read loop if reading breaks (e.g. EOF)

    i=i+1
    j=j+1 -- this might be missing from your original source?!

    coor(1,i,j)=d(1)
    coor(2,i,j)=d(2)
    coor(3,i,j)=d(3)

    write(6,20), coor(1,i,j),coor(2,i,j),coor(3,i,j)
end do

根据gfortran的文档和 ifort Fortran 2003 标准中有一个名为 IS_IOSTAT_END 的函数。不幸的是,我找不到它的规范的位置。然而,根据the gfortran documentation “该函数相当于将变量与内部模块 ISO_FORTRAN_ENVIOSTAT_END 参数进行比较”,这确实是标准化的。

关于linux - forrtl : severe (174): SIGSEGV, 发生段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062668/

相关文章:

mysql - MariaDB格式化查看代码

fortran - 从 Fortran 模块中定义的函数打印到标准输出

fortran - 为什么将 "complex*16"更改为 "complex(16)"会导致fortran中运行时间不合理地增加?

linux - puppet factor 上次运行状态

PHP 站点在 linux 上运行但不能在 windows 上运行

linux - 在端口 10334 连接到定位器的第二个 geode 容器的问题

arrays - 在数组中查找满足 Fortran 条件的 [index of] 最小值

c# - 在 VS2019 中使用 iFort 从 C# 调用 Fortran

fortran - 英特尔 Fortran 值属性

linux - 使用 top 从 PID 和 COMMAND 获取前 5 行