fortran - 检查传递给函数的数组的大小

标签 fortran

我在一个大程序中发现了一个错误,其中一个子程序被写为:

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/

    相关文章:

    在 Fortran 中调用 C 函数,其中参数是指针

    gcc - gfortran编译器错误: result of exponentiation exceeds the range of INTEGER(4)

    fortran - 求解二次方程但得到奇怪的错误

    types - 带 MPI 的派生数据类型

    arrays - 如何在 Fortran 中声明数组变量及其大小中间例程

    global-variables - Fortran 中 protected 全局变量

    python - 仅在内部使用派生类型的 F2PY 包装过程

    parsing - 使用 Happy (Haskell) 从 yacc 语法生成 Fortran 77 解析器

    ide - 具有良好的fortran支持的编辑器

    fortran - Fortran 中的 OpenMP