我在一个子程序编写为的大型程序中发现了一个错误:
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
Run Code Online (Sandbox Code Playgroud)
即使代码显然存在错误,该程序也可以与ifort和gfortran一起运行并进行边界检查。是否可以捕获此类错误?
PS:这是生成可以正常运行的二进制文件的两个命令
ifort -check all main.f90 -o maingfortran -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
Run Code Online (Sandbox Code Playgroud)
gfortran 6.1.0
Fortran runtime error: Index '7' of dimension 1 of array 'x' above upper bound of 6
Run Code Online (Sandbox Code Playgroud)
而当它和假定的大小 ( dimension(*)) 或明确的形状 ( dimension(n)) 时,则不是。它可能是故意设计的。
ifort手册
对于最后一个维度边界指定为 * 或上限和下限均为 1 的虚拟参数数组,不会执行数组边界检查。
gfortran 手册没有详细说明这一点