- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
[fortran]
subroutine foo(data, n, m)
integer, intent(in) :: n, m
integer,dimension(n,m), intent(in) :: data
end subroutine foo
[/fortran]
the second is assumed-shape dummy arrays, but this method require the explicit interface of the subroutine
[fortran]
subroutine foo(data)
integer,dimension(:,:), intent(in) :: data
end subroutine foo
[/fortran]
the third is assumed-size dummy arrays, which declare all the dimension of the array except the last dimention.
[fortran]
subroutine foo(data)
integer,dimension(5,*), intent(in) :: data
end subroutine foo
[/fortran]
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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".
3. ... 4. if the 3rd is right, how does the compiler know the total size of the arrayIt 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page