- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I wonder how to make the array `array1` be global (module) variable, consider following code
module data implicit none real(8), allocatable, dimension(:,:) :: A end module data subroutine test(array1, n, m) use data implicit none integer, intent(in) :: n, m real(8), intent(in), dimension(n,m) :: array1 allocate(A, source=array1) do_something end subroutine test
`allocate(A, source=array1)` works but double store the same array, `save` is another method but not satisfied.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am not entirely sure what you want to achieve, but you could define the array A to be a pointer:
module .. real(8), dimension(:,:), pointer :: A end module subroutine ... real(8), dimension(:,:), target :: array1 A => array1 end subroutine
You have only one copy of the array then, but make sure that the array1 remains "alive" as long as you want to use A, while pointing to array1.
It might be better to move the allocation, via call move_alloc - array1 must be an allocatable array/pointer to an array. That way you transfer the memory to A and you only have to worry about the "aliveness" of A.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am not entirely sure what you want to achieve, but you could define the array A to be a pointer:
module .. real(8), dimension(:,:), pointer :: A end module subroutine ... real(8), dimension(:,:), target :: array1 A => array1 end subroutine
You have only one copy of the array then, but make sure that the array1 remains "alive" as long as you want to use A, while pointing to array1.
It might be better to move the allocation, via call move_alloc - array1 must be an allocatable array/pointer to an array. That way you transfer the memory to A and you only have to worry about the "aliveness" of A.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
A is already "global" - I too am not sure what you want to happen differently. As you have it, the allocate of the global array A occurs in the subroutine and the dummy argument array1 is copied into it - that's what source= does. There is no double store. What would you like to happen? Really, you don't need subroutine test at all - the ALLOCATE will do everything.
Do not use pointers here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sure, A is already global, but does not initialized, I just want dummy argument array1 be global, and the subroutine test will be called by other language.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Arjen Markus wrote:
I am not entirely sure what you want to achieve, but you could define the array A to be a pointer:
module .. real(8), dimension(:,:), pointer :: A end module subroutine ... real(8), dimension(:,:), target :: array1 A => array1 end subroutineYou have only one copy of the array then, but make sure that the array1 remains "alive" as long as you want to use A, while pointing to array1.
It might be better to move the allocation, via call move_alloc - array1 must be an allocatable array/pointer to an array. That way you transfer the memory to A and you only have to worry about the "aliveness" of A.
Many thanks, I have tested the pointer solution, yes, only one copy of the array.

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