Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29437 Discussions

Pointer to non-interoperable polymorphic entity clarification

ivanp
New Contributor I
93 Views

Is the program below legal?

program test1
use, intrinsic :: iso_c_binding, only: c_loc
implicit none

type :: foo
   integer :: b(3)
end type

type(foo), target :: x
class(foo), pointer :: p

! Potentially illegal, fptr should be nonpolymorphic
call c_f_pointer(cptr=c_loc(x), fptr=p)  

! J3/24-007, page 504, L40-L42, Case (ii)
! If the value of CPTR is the result of a reference to C_LOC with
! a noninteroperable effective argument X, FPTR shall be a nonpolymorphic pointer 
! with the same type and type parameters as X.

print *, same_type_as(p, x)

end program


With ifort the program prints "true". With ifx 2025.3 it also prints "true". Is this behavior portable? 

I see one potential loophole if the association is covered by Case (iii) of `c_f_pointer` (J3/24-007, page 505), i.e.

"If the value of CPTR is the C address of a storage sequence that is not in use by any other Fortran entity, FPTR becomes associated with that storage sequence. The storage sequence shall be large enough to contain the target object described by FPTR and shall satisfy any other processor-dependent requirement for association."

Could anyone clarify if this is the case here?

0 Kudos
0 Replies
Reply