Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
22 Views

Make intent in array be global

Jump to solution

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.

0 Kudos

Accepted Solutions
Highlighted
Valued Contributor II
22 Views

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.

View solution in original post

0 Kudos
4 Replies
Highlighted
Valued Contributor II
23 Views

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.

View solution in original post

0 Kudos
Highlighted
Black Belt Retired Employee
22 Views

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.

0 Kudos
Highlighted
Beginner
22 Views

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. 

0 Kudos
Highlighted
Beginner
22 Views

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 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.

Many thanks, I have tested the pointer solution, yes, only one copy of the array.

0 Kudos