keeping array limits in fortran during subroutine call

后端 未结 3 471
执笔经年
执笔经年 2021-01-19 00:56

I have the following program

module test
contains
   subroutine foo()
      integer, allocatable :: a(:)
      allocate(a(-5:5))
      call bar(a)
      prin         


        
相关标签:
3条回答
  • 2021-01-19 01:20

    There are two common options:

    • As kemisto wrote, you pass a second argument. This was common in F77-style code. You can not use the LBOUND trick! It has to be passed as an integer.
    • You declare the argument to be a pointer, which includes the entire array descriptor. Then the bounds of the array in the subroutine are the same as in the calling scope. Of course you may lose on optimization this way.
    0 讨论(0)
  • 2021-01-19 01:21

    The type of dummy argument that you are are using in the subroutine, with the dimension specified with a colon, is called "assumed shape". This name is the clue -- Fortran passes only the shape and not the lower and upper bounds. The lower bound is assumed to be one unless you override it as shown in the answer by kemiisto. If the lower bound is not fixed, you can pass an argument to use as the lower bound.

    Later addition: a code example if the lower dimension isn't known at compile time:

    subroutine example (low, array)
       integer, intent (in) :: low
       real, dimension (low:), intent (out) :: array
    
    0 讨论(0)
  • 2021-01-19 01:37

    How can I instruct the compiler that, within the bar routine, the numbering of the a array must be the same as in the caller ?

    Not sure but according to the standard you can specify the lower bound for an assumed-shape array.

    subroutine bar(a)
          integer, intent(out) :: a(-5:)
    
    0 讨论(0)
提交回复
热议问题