a question about pass multi dimension array to subroutine.

a question about pass multi dimension array to subroutine.

Hi, I want to ask a question about pass multi dimension array to subroutine.

I know there are three ways to achieve this goal. First is the explicit-shape dummy array
the example code is below:

subroutine foo(data, n, m)
    integer, intent(in) :: n, m
    integer,dimension(n,m), intent(in) :: data
end subroutine foo

the second is assumed-shape dummy arrays, but this method require the explicit interface of the subroutine

subroutine foo(data)
    integer,dimension(:,:), intent(in) :: data
end subroutine foo

the third is assumed-size dummy arrays, which declare all the dimension of the array except the last dimention.

subroutine foo(data)
    integer,dimension(5,*), intent(in) :: data
end subroutine foo

I have some question:
1. the summarize above was right?
2. In the second method, I think the compiler actually pass the dimension method in the dummy argument, just like the stdcall reference calling convention, am i right?
3. How does the third method achieve the goal? I thought the compiler knew the total size of the array, the last dimension was calculated by division. am i right?
4. if the 3rd is right, how does the compiler know the total size of the array, especially when the subroutine is not explicit.

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

In your last example, you don't show any way to know the array size either at compile or run time.  There would be relatively limited possibilities in how you could operate on the array.

In the first example, if you don't have bounds checking or whole-array assignment, it may make no difference whether you declare dimension(n,m) or dimension(n,*).  In Fortran 90, the * is called assumed size (it's assumed that the array is made sufficiently large by the caller).  You would not be able to use a whole-array operation such as data = 2 * data, but you could use data(:,:m) = 2 * data(:,:m)

Quote:

I know there are three ways to achieve this goal.
You have not stated the goal. Is it just to pass an array, or to be able to know all the attributes of the array in the subroutine? I cannot make sense of "actually pass the dimension method in the dummy argument". Quote:
3. ... 4. if the 3rd is right, how does the compiler know the total size of the array
It does not know the size. That is why subroutines that take array arguments need to be also provided arguments or access to COMMON or module variables that contain this information.

mecej

Thank you for your help!

1. I apologize, what i mean is " I think the compiler actually pass the dimension infomation in the dummy argument, just like the stdcall reference calling convention"

2. the goal is to pass an array, and use it in the subroutine

Leave a Comment

Please sign in to add a comment. Not a member? Join today