Fortran 90 - “分段错误 - 无效的内存引用"具有可扩展的 3D 阵列 [英] Fortran 90 - "Segmentation fault - invalid memory reference" with scalable 3D array

查看:10
本文介绍了Fortran 90 - “分段错误 - 无效的内存引用"具有可扩展的 3D 阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 gfortran 编译了一个 fortran 90 程序,它以我想要的方式构建了一个可扩展的 3D 数组.运行时出现以下错误:

I have compiled a fortran 90 program with gfortran which builds a scalable 3D array in a way I want. Upon running, I get the following error:

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

Backtrace for this error:
#0  0x10542ee42
#1  0x10542f60e
#2  0x7fff8d7895a9
#3  0x10542575e
#4  0x105425975
#5  0x105425d0e
Segmentation fault: 11

我认为这是大型 3D 阵列的内存问题,因为如果我减小尺寸,它会起作用,但有没有办法解决这个问题?这是我的代码:

I believe this is a memory issue with the large 3D array, as it works if I decrease the dimensions, but is there anyway to get around this? Here is my code:

PROGRAM phantomtest
IMPLICIT NONE
  INTEGER, PARAMETER:: columns=34, rows=34, diags=((4*columns)-6),  m=(4*columns)-6+(2*columns)
  REAL, ALLOCATABLE, DIMENSION(:,:,:)::phantom
  INTEGER :: i, j, k
  CHARACTER (LEN=3) :: nstring, nullstring=''

ALLOCATE(phantom(columns,rows,m))
phantom=0

CALL Phantom_Making(phantom,columns,rows,diags,m)

WRITE(nstring,FMT="(I3)"), columns
PRINT*, nullstring
DO k=1,m
  DO i=1,columns
    WRITE(*,FMT="("//nstring//"I2)") phantom(i,:,k)
  END DO
  PRINT *, nullstring
END DO

END PROGRAM phantomtest
!---------------------------
SUBROUTINE Phantom_Making(phantom,columns,rows,diags,m)
IMPLICIT NONE
  INTEGER, INTENT(IN):: columns, rows, diags, m
  REAL, DIMENSION(columns,rows,m), INTENT(INOUT) :: phantom
  INTEGER :: i, j, k, l

!Vertical and horizontal rays
DO i=1,rows
  phantom(:,i,i) = 1
  phantom(i,:,i+(columns)+(diags/2)) = 1
END DO

!Diagonal rays
phantom(1,2,1+columns) = 1
phantom(2,1,1+columns) = 1
phantom(1,columns-1,1+columns+(diags/2)+rows) = 1
phantom(2,columns,1+columns+(diags/2)+rows) = 1
j = columns-1

DO k=2+columns, (diags/2)+columns
  phantom(2:columns,:,k) = phantom(1:(columns-1),:,k-1)
  IF (((k+1)-columns).LE.columns) phantom(1,k+1-columns,k)=1
END DO

DO l=columns+(diags/2)+rows+2, columns+(diags/2)+rows+1+(diags/2)
  j = j-1
  phantom(2:columns,:,l) = phantom(1:(columns-1),:,l-1)
  IF (j.GT.0) phantom(1,j,l) = 1
END DO

END SUBROUTINE

推荐答案

许多编译器都有的检查可以帮助你调试.以 gfortran 为例:

The checks which many compilers have can assist you in debugging. For example with gfortran:

gfortran -g -fcheck=all -Wall segf.f90
segf.f90:5.17:

  INTEGER :: i, j, k
                 1
Warning: Unused variable 'j' declared at (1)
~/f/testy/stackoverflow> ./a.out 
At line 50 of file segf.f90
Fortran runtime error: Index '199' of dimension 3 of array 'phantom' outside of expected range (1:198)

其他编译器也有类似的选项.

Other compilers have similar options.

您的循环在 l 的此类值上运行,您正在访问数组phantom 超出范围.

Your loops runs on such values of l that you are accessing the arrayphantom out of bounds.

确保 l 具有正确的值,1:m,或者数组幻像的维度被正确指定为这个范围(1:m).

Make sure l has the correct values, 1:m, or that the dimensions of the array phantom are specified correctly to be this range (1:m).

这篇关于Fortran 90 - “分段错误 - 无效的内存引用"具有可扩展的 3D 阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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