我在一个大程序中发现了一个错误,其中一个子程序被写为:
program main
implicit none
real, dimension(6) :: x
call f(x, 7)
write (*,*) x
contains
subroutine f(x, n)
integer :: n
real, dimension(n) :: x
integer :: i
do i = 1, n
x(i) = 0.0
end do
end subroutine f
end program main
即使代码明显有缺陷,该程序在 ifort 和 gfortran 以及边界检查下都可以正常运行。有没有办法捕捉这些错误?
PS:这里是生成运行良好的二进制文件的两个命令
ifort -check all main.f90 -o main
gfortran -fbounds-check main.f90 -o main
最佳答案
免责声明:作者要求提供一个选项来捕获错误。这个答案没有提供。
如果数组是假定的形状( dimension(:)
),则执行检查:
堡垒 15.5
forrtl: severe (408): fort: (2): Subscript #1 of the array X has value 7 which is greater than the upper bound of 6
gfortran 6.1.0
Fortran runtime error: Index '7' of dimension 1 of array 'x' above upper bound of 6
而当它的和假定的大小(
dimension(*)
)或明确的形状( dimension(n)
)时,它不是。它可能是有意设计的。堡垒机
Array bounds checking is not performed for arrays that are dummy arguments in which the last dimension bound is specified as * or when both upper and lower dimensions are 1.
gfortran 手册没有详细说明这一点
关于fortran - 检查传递给函数的数组的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809409/