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
551 views
in Technique[技术] by (71.8m points)

fortran - What is the effect of passing an allocatable variable into a subroutine with non-allocatable argument?

Say we have this variable definition

Real*8, Dimension(:), Allocatable :: dblA
Allocate  (dblA(1000))

Now I call this subroutine:

Call MySub(dblA)

In which:

Subroutine MySub(dblA)
Real*8,  INTENT(Out), DIMENSION(1000) :: dblA
End

What is the side effect of such practice? Should I avoid this?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

If the array has been allocated prior to being passed to the subroutine the subroutine is indifferent to the allocatable-ness, the effect is the same as if the array were static. There is nothing to object to, nor practice to avoid, in the snippets you show us. However, those snippets actually do very little and it is easy to think of ways to extend them to invalidate this advice.

Now do yourself a favour and change

Real*8,  INTENT(Out), DIMENSION(1000) :: dblA

to

Real*8,  INTENT(Out), DIMENSION(:) :: dblA

so that the subroutine works correctly whatever the size of the array it gets passed. It's probably also a bad idea to use dblA as the name for both the dummy and actual arguments, you'll just confuse yourself.

And real*8 is not, and never has been, a standard-compliant way of declaring an 8-byte real. On this point, see numerous questions and answers here on SO.


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

...