arrays - 在子程序调用期间保持 Fortran 中的数组限制

标签 arrays fortran fortran90

我有以下程序

module test
contains
   subroutine foo()
      integer, allocatable :: a(:)
      allocate(a(-5:5))
      call bar(a)
      print *, a
   end subroutine
   subroutine bar(a)
      integer, intent(out) :: a(:)
      a = 0 
      a(-4) = 3  ! here
      a(2) = 3   
   end subroutine
end module

program x
   use test
   call foo()
end program

在标有“这里”的行中,我做错了什么。事实是,当我收到数组 a 时(在从 -5 到 +5 分配的调用者中),被调用者使用常规编号(1 到 n),这意味着分配 -4 我正在执行越界分配。我如何指示编译器在 bar 内例程,a的编号数组必须与调用者中的相同吗?

最佳答案

您在子例程中使用的虚拟参数类型,其维度用冒号指定,称为“假定形状”。这个名字就是线索——Fortran 只传递形状而不是上下限。下限假定为 1,除非您按照 kemiisto 的回答中所示覆盖它。如果下限不固定,您可以传递参数以用作下限。

后来添加:如果在编译时不知道较低维度的代码示例:

subroutine example (low, array)
   integer, intent (in) :: low
   real, dimension (low:), intent (out) :: array

关于arrays - 在子程序调用期间保持 Fortran 中的数组限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4366495/

相关文章:

c++ - 使 C++ 对象数组在 Python 中可迭代

java - 在对象的数组列表中查找最小值,无法比较数组列表中的元素

c - C中求数组到0为止的最大、最小、总和、平均值

visual-studio - QR分解Fortran错误

c - 从 Fortran 中的 C 二进制文件读取 float

系统中精度最高的Fortran格式语句

javascript - 使用一个键的值数组过滤多个 javascript 对象

fortran - 将 Fortran 例程作为 Scheme 函数调用

Python 读取未格式化的直接访问 Fortran 90 给出不正确的输出

Fortran 错误 : Unit connected to formatted file