Pointer remapping and ASSOCIATED

For the code below, gfortran 12.0.1 20220213 and Intel Fortran 2021.5.0 Build 20211109_000000 give different results for the last 2 cases, with ifort giving T and gfortran F. I wonder who is correct.

program test_associated ! illustrate rules at
! https://gcc.gnu.org/onlinedocs/gcc-8.4.0/gfortran/ASSOCIATED.html
implicit none
integer, parameter :: n = 4
integer, target :: i,j,k(0),m(n),a(2,2)
integer, pointer :: p => null(), q=> null(), r(:) => null(), &
  s(:) => null(), t(:) => null(), u(:) => null(), v(:) => null(), &
  w(:) => null(), x(:,:) => null(), y(:,:) => null()
p => i
r => k
s => m
t => m
u(0:3) => m
v => m(n:1:-1)
w => m(2:)
x(1:4,1:1) => a
y(1:1,1:4) => a
! comments show Intel Fortran results
print*,associated(p)     ! T : p is associated
print*,associated(p,i)   ! T : p is associated to scalar i
print*,associated(p,j)   ! F : p is not associated to j
print*,associated(q)     ! F : q is not associated
print*,associated(r)     ! T : r is associated
print*,associated(r,k)   ! F : the target of r is k with zero size
print*,associated(s)     ! T : s is associated
print*,associated(s,m)   ! T : s is associated to m with nonzero size
print*,associated(s,t)   ! T : s and t both associated to m
print*,associated(s,u)   ! T : s and u both associated to m
print*,associated(v,m)   ! F : v associated to reverse order of m
print*,associated(w,m)   ! F : w and m have different size
print*,associated(x,a)   ! T : x associated to a
print*,associated(x,y)   ! T : x and y associated to a
end program test_associated

The rules for ASSOCIATED, according to gfortran docs, are

Description :

ASSOCIATED(PTR [, TGT]) determines the status of the pointer PTR or if PTR is associated with the target TGT.

Standard :

F95 and later

Class :

Inquiry function

Syntax :

RESULT = ASSOCIATED(PTR [, TGT])

Arguments :

PTR PTR shall have the POINTER attribute and it can be of any type.
TGT (Optional) TGT shall be a POINTER or a TARGET . It must have the same type, kind type parameter, and array rank as PTR.

The status of neither PTR nor TGT can be undefined.

Return value :

ASSOCIATED(PTR) returns a scalar value of type LOGICAL(4) . There are several cases:

(A) If the optional TGT is not present, then ASSOCIATED(PTR)

is true if PTR is associated with a target; otherwise, it returns false.

(B) If TGT is present and a scalar target, the result is true if

TGT is not a 0 sized storage sequence and the target associated with PTR occupies the same storage units. If PTR is disassociated, then the result is false.

(C) If TGT is present and an array target, the result is true if

TGT and PTR have the same shape, are not 0 sized arrays, are arrays whose elements are not 0 sized storage sequences, and TGT and PTR occupy the same storage units in array element order. As in case(B), the result is false, if PTR is disassociated.

(D) If TGT is present and an scalar pointer, the result is true if

target associated with PTR and the target associated with TGT are not 0 sized storage sequences and occupy the same storage units. The result is false, if either TGT or PTR is disassociated.

(E) If TGT is present and an array pointer, the result is true if

target associated with PTR and the target associated with TGT have the same shape, are not 0 sized arrays, are arrays whose elements are not 0 sized storage sequences, and TGT and PTR occupy the same storage units in array element order. The result is false, if either TGT or PTR is disassociated.

For the line print*,associated(x,a) , looking at rule (C) above, since x and a have different shapes I think gfortran is correct to print F, but ifort says T. For the last line print*,associated(x,y), looking at rule (E) above, I think ifort is correct to say T since the conditions are satisfied.

The Standard (J3/18-007r1) I have in front of me has different wording.

Instead of (C) and (E) above, I see (with TARGET/TGT POINTER/PTR rewording):

Case (v): If TARGET is present and is an array target, the result is true if 
and only if POINTER is associated with a target that has the same shape as 
TARGET, is neither of size zero nor an array whose elements are zero-sized 
storage sequences, and occupies the same storage units as TARGET in array 
element order.
...
Case (vii): If TARGET is present and is an array pointer, the result is true 
if and only if POINTER and TARGET are both associated, have the same shape, 
are neither of size zero nor arrays whose elements are zero-sized storage 
sequences, and occupy the same storage units in array element order.

On the basis of the words of the Standard,

is false (different shapes) and so is

for the same reason. You can ask for the shapes to be printed.

1 Like