Recently I noticed that it is possible to associate a pointer with a data object without the target attribute through the use of a subroutine. I tested this with a 4×4 array arr
by associating ptr_to_arr => arr
and then by performing subtraction arr-ptr_to_arr
, which yielded 0.0
everywhere.
Because only the dummy argument of subroutine assign_ptr
and not the actual argument has the target attribute, I expected the pointer to become disassociated outside the subroutine’s scope. However it remains associated.
Can someone say whether this conforms to the Fortran standard or whether this is a bug? I am using GCC 11.4.0.
! ---------------------------
! MODULE CONTAINING PROCEDURE
! ---------------------------
module assign_ptr_mod
implicit none
contains
subroutine assign_ptr (arr, ptr)
implicit none
real, dimension (:, :), target, intent (in) :: arr
real, dimension (:, :), pointer, intent (out) :: ptr
ptr => arr
end subroutine assign_ptr
end module assign_ptr_mod
! -------------
! BEGIN PROGRAM
! -------------
program main
use assign_ptr_mod, only : assign_ptr
implicit none
integer, parameter :: nx = 4, ny = 4
integer :: j
real, dimension (:, :), allocatable :: arr
real, dimension (:, :), pointer :: ptr_to_arr
! Populate array with random numbers
allocate (arr(nx, ny))
call random_number (arr)
call assign_ptr (arr, ptr_to_arr)
print *, '-------------------------'
print *, 'Is ptr_to_arr associated?'
print *, '-------------------------'
print *, 'Association status is ', associated (ptr_to_arr)
print *
print *, '------------------------------------------------'
print *, 'INITIAL ARRAY VALUES BY RANDOM NUMBER GENERATOR'
print *, '------------------------------------------------'
call printer ()
! Repopulate array with random numbers
call random_number (arr)
print *, '------------------------------------------------'
print *, 'UPDATED ARRAY VALUES BY RANDOM NUMBER GENERATOR'
print *, '------------------------------------------------'
call printer ()
contains
subroutine printer
implicit none
print *, 'array values are'
print *
do j = ny, 1, -1
print *, arr(:, j)
end do
print *
print *, 'ptr values are'
print *
do j = ny, 1, -1
print *, ptr_to_arr(:, j)
end do
print *
print *, 'substract ptr values from arr (should yield zero)'
print *
do j = ny, 1, -1
print *, arr(:, j) - ptr_to_arr(:, j)
end do
print *
end subroutine printer
end program main