由于内存访问问题导致的分段故障

Segmentation fault due to memory access issue

提问人:Surya Sarvajith 提问时间:7/30/2022 最后编辑:Surya Sarvajith 更新时间:7/30/2022 访问量:85

问:

当我运行 f77 代码时,由于内存访问问题,我遇到了分段错误。

这是我的终端输出的消息:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000f523b7 in bcvnmtn (xb=<error reading variable: Cannot access memory at address 0xc44a8000c3818>, 
    yb=<error reading variable: Cannot access memory at address 0xc5dc8000c5138>, nstp=0, npoints=0, tx=..., ty=..., anx=..., any=...) at ./bcvnmtn.F:14
14            tx(1) = xb(1+nstp) - xb(1)

下面是遇到此分段错误的子例程

      subroutine bcvnmtn(xb,yb,nstp,npoints,tx,ty,anx,any)

c-----compute the vertex valued tangential and normal vector fields
c----- (tx,ty)(n) and (anx,any)(n) to the curve (xb,yb)((n-1)*nstp+1)
c----- for n=1 to npoints

cdir$ nolist
      include 'paramcom.h'
cdir$ list
      dimension xb(1),yb(1),tx(1),ty(1),anx(1),any(1)

c-----get tangent vector field

      tx(1) = xb(1+nstp) - xb(1)
      ty(1) = yb(1+nstp) - yb(1)

      in = 1
      do 100 n=2,npoints-1
        inp = in + 2*nstp
        tx(n) = (xb(inp) - xb(in))*0.5
        ty(n) = (yb(inp) - yb(in))*0.5
        in = in + nstp
  100 continue

      nlast = npoints
      ilast = 1 + (npoints-1)*nstp
      tx(nlast) = xb(ilast) - xb(ilast-nstp)
      ty(nlast) = yb(ilast) - yb(ilast-nstp)

c-----normalize to unit length and rotate to get normal vector

      do 200 n=1,npoints
        tl = sqrt( tx(n)**2 + ty(n)**2 )
        tx(n) = tx(n) / ( tl + tiny )
        ty(n) = ty(n) / ( tl + tiny )
        any(n) = tx(n)
        anx(n) = -ty(n)
  200 continue

      return
      end

上面的子例程由以下子例程调用:

      subroutine bcsymgd(xi,yi)

c-----compute ghost point locations along a symmetry boundary

cdir$ nolist
      include 'common.h'
      include 'inputcom.h'
      include 'pointer.h'
cdir$ list
      common /bcpoint/ idimfrm,jdimfrm,isttfrm,jsttfrm,nstpfrm,nofffrm,
     %                 idimtoo,jdimtoo,istttoo,jstttoo,nstptoo,nofftoo,
     %                 npoints
      dimension xi(0:idimfrm,0:jdimfrm),yi(0:idimfrm,0:jdimfrm)
      call bcvnmtn(xi(isttfrm,jsttfrm),yi(isttfrm,jsttfrm),
     %             nstpfrm,npoints,tangx,tangy,anormx,anormy)
      call bcdifvf(dispx(isttfrm,jsttfrm),dispy(isttfrm,jsttfrm),
     %             nofffrm,nstpfrm,
     %             xi(isttfrm,jsttfrm),yi(isttfrm,jsttfrm),
     %             (- nofftoo),nstpfrm,
     %             xi(isttfrm,jsttfrm),yi(isttfrm,jsttfrm),
     %             nofffrm,nstpfrm,npoints)
      call bctrflc(dispx(isttfrm,jsttfrm),dispy(isttfrm,jsttfrm),
     %             dispx(isttfrm,jsttfrm),dispy(isttfrm,jsttfrm),
     %             nstpfrm,npoints,tangx,tangy,anormx,anormy)
      call bcaddvf(xi(istttoo,jstttoo),yi(istttoo,jstttoo),
     %             nofftoo,nstptoo,
     %             dispx(isttfrm,jsttfrm),dispy(isttfrm,jsttfrm),
     %             nofffrm,nstpfrm,
     %             xi(isttfrm,jsttfrm),yi(isttfrm,jsttfrm),
     %             nofffrm,nstpfrm,npoints)

      return
      end

习 和 yi (在 bcymgd.F) 是几何图形的坐标。但正如你所看到的,它似乎是从 bcymgd 的 2 维开始的。F 到 bcvnmtn 中的 1 维 (xb,yb)。F

这是一个非常大和旧的代码。我目前没有太多的灵活性来编辑源代码。我正在使用 pgi/2020.4 编译器。

如果有人能帮我解决这个问题,那就太好了。如果我能提供更多信息,请告诉我。

分段故障 FORTRAN 指令 FORTRAN77

评论

0赞 Vladimir F Героям слава 7/30/2022
对于所有 Fortran 问题,请使用标签 fortran。没有它,只有少数人会看到你的问题。
3赞 Vladimir F Героям слава 7/30/2022
若要获取相关的回溯,请使用标志使用调试符号进行编译。同时启用编译器提供的所有调试检查。特别是启用数组边界检查。-g
0赞 Surya Sarvajith 7/30/2022
我将尝试数组绑定检查,看看是否有帮助。每次我运行代码时,它都会出现相同的错误。我使用的输入参数是作者提供的测试运行包。我已经浏览了该输入参数包,没有任何问题。我没有对代码进行任何逻辑更改。代码使用了一些 C 文件,我不得不包含一些头文件来删除一些警告。
0赞 veryreverie 7/31/2022
如果没有给你回溯(在某些 Windows 设置中似乎没有),你也可以尝试通过 gdb 运行你的代码-g
0赞 cup 3/13/2023
如果 xb 的数组绑定为 1,而 nstp 不是零,则数组将越界。您可以尝试使用 * 而不是 1 来绑定数组,但根据 xb 的位数,您仍然可能会遇到段错误。

答: 暂无答案