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

Assigning object address to pointer to polymorphic object

ZlamalJakub
New Contributor III
574 Views
I want to store address of polymorphic object in my data structures (it will point to different types of objects). Then when I want to use this object I want to assign this memory to pointer and start to work with this object but I am able to do it only using cheat function assignpointertomem:
[fortran]MODULE Test IMPLICIT NONE TYPE,PUBLIC :: T_TST INTEGER :: Var CONTAINS PROCEDURE,PUBLIC :: SetVar => SetVar END TYPE T_TST CONTAINS SUBROUTINE SetVar(this,iAux) IMPLICIT NONE CLASS(T_TST) :: this integer(4) :: iAux this%Var = iAux END SUBROUTINE END MODULE test module interfaces interface ! interface to function assigning pointer subroutine assingPointerToMem(Ptr,pMem) use ifwinty use test type(T_TST), pointer :: Ptr integer(LPVOID) pMem ! HANDLE instead of "class(T_TST), target::" to cheat compiler end subroutine end interface end module subroutine assingPointerToMem(Ptr,pMem) use test class(T_TST), pointer :: Ptr class(T_TST), target :: pMem Ptr=>pMem end subroutine PROGRAM TestClass USE Test use ifwinty use interfaces IMPLICIT NONE integer(LPVOID) pMem TYPE(T_TST), target :: tst TYPE(T_TST) :: tstpointer pointer (ptstpointer,tstpointer) type(T_TST), pointer :: tstpointer2 type(T_TST), pointer :: tstpointer3 pMem=LOC(tst) ptstpointer=pMem tstpointer2=>tst call assingPointerToMem(tstpointer3,pMem) ! use assingPointerToMem to assign memory location to pointer CALL tst%SetVar(3) CALL tstpointer2%SetVar(4) ! works well CALL tstpointer3%SetVar(5) ! works well CALL tstpointer%SetVar(6) ! calling this routine causes program crash (this variable is unassigned in SetVar) end program TestClass [/fortran] I am surprised that [fortran]CALL tstpointer%SetVar(6)[/fortran] not work. Is it possible to use integer pointer to call polymorphic object?
0 Kudos
3 Replies
IanH
Honored Contributor III
574 Views
A fortran pointer to a polymorphic scalar cannot be simply represented as an integer - the code needs to track both the location of the data for the object and the dynamic type of the object. I doubt very much that your interface mismatch achieves what you want.

0 Kudos
ZlamalJakub
New Contributor III
574 Views
When I tested assembly of code
[fortran]tstpointer2=>tst
00271123 mov dword ptr [TSTPOINTER2 (342648h)],offset TST (342640h)
[/fortran] It only copy address of tst to tstpointer2 nothing else, so I hope it is correct to use integer pointers.
0 Kudos
IanH
Honored Contributor III
574 Views
tstpointer2 isn't a polymorphic pointer - it is declared with TYPE, not CLASS. For non-polymorphic pointers the dynamic type is the same as the declared type, so the compiler just needs to "remember" the location of the object's data.
0 Kudos
Reply