Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

fortran, passing allocatable arrays to a subroutine with right bounds

I need in a program to pass some allocatable arrays to subroutines, and i need to know if the way I do it are in the standard or not.

If you know where I can search for the standard of fortran, Tell me please.

Here is a little code that will better explain than words

program test

use modt99

implicit none

real(pr), dimension(:), allocatable :: vx

allocate(vx(-1:6))
vx=(/666,214,558,332,-521,-999,120,55/)
call test3(vx,vx,vx)
deallocate(vx)

end program test

with the module modt99

module modt99

contains
subroutine test3(v1,v2,v3)
  real(pr), dimension(:), intent(in) :: v1
  real(pr), dimension(0:), intent(in) :: v2
  real(pr), dimension(:), allocatable, intent(in) :: v3

  print*,'================================'
  print*,v1(1:3)
  print*,'================================'
  print*,v2(1:3)
  print*,'================================'
  print*,v3(1:3)
  print*,'================================'

end subroutine test3


end module modt99

on screen, I get

 ================================
   666.000000000000        214.000000000000        558.000000000000     
 ================================
   214.000000000000        558.000000000000        332.000000000000     
 ================================
   558.000000000000        332.000000000000       -521.000000000000     
 ================================

So are the three ways of dummy arguments in subroutine test3 legal (in what version of fortran, 90, 95, 2003?) and are their behavior normal?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first version passes the array slice to the subroutine. Note that boundary information are not passed along in this way, arrays are assumed to start at 1 and go to size(array).

The second way is just like the first one, but you manually set the lower boundary to 0, that's why printing v3(1:3) gives you the values with an offset of 1.

The third way passes all array information to the subroutine (including boundaries), hence the "correct" indexing. Passing allocatable arrays was introduced with Fortran 2003.

Apart from the fact that you have an aliasing issue (passing the same variable to three different dummy arguments), all three versions are legal.

You can find all documents of the standards here.

Especially, take a look at the Fortran 2003 Standard, Ch. 5.1.2.5 DIMENSION attribute to see the differences between assumed shape and deferred shape arrays in dummy arguments.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...