Quantcast
Channel: Intel® Fortran Composer XE
Viewing all articles
Browse latest Browse all 1424

Allocatable Array of Inherited Derived Types Issues in Fortran

$
0
0

I'm attempting to create global-ish-ly available allocatable array of a set of derived types that share inheritance with a single object. Fortran does not seem to make this very easy. The below is what I have so far.

First the derived types and module with the allocatable array.

    Module Environment

        use Entity_M
        type(Entity_C), dimenion(:), allocatable :: objects
    End Module Environment

    Module Entity_M
        type Entity_T
            integer :: id
            real*8 :: time
            real*8, dimension(3) :: currPos

            type(TrajectoryDatum), dimension(:), allocatable :: trajTable

        end type Entity_T

        type Entity_C
            class(Entity_T), pointer :: e
        end type Entity_C

        type, extends(Entity_T) :: Aircraft_T
            real*8 :: altitude
        end type Aircraft_T

        type, extends(Entity_T) :: Missile_T
            integer :: targetID
        end type Missile_T

    End Module Entity_M

Now the main program

   

 Program Main

        use Initialization
        use Environment
        use Entity_M

        call simInit(3)
        write(*,*) objects%trajTable !<---- this does not persist

        call runSim()

    End Program Main

The code with the issue

    

Module Initialization

        use Entity_M

        contains

        subroutine simInit(numOfObjects)

            integer, intent(in) :: numOfObjects

            call objectsInit(numOfObjects)
            call launchersInit()

        end subroutine simInit


        subroutine objectsInit(numOfObjects)

            use Environment

            integer, intent(in) :: numOfObjects

            !local
            type(Aircraft_T) :: aircraft
            integer :: i

            allocate(objects(numOfObjects)

            do i = 1, numOfObjects

                aircraft%trajTable = getTrajectoryData()

                call allocatePointer(objects(i)%e, aircraft)

            end do

        end subroutine objectsInit

        subroutine allocatePointer(c, t)

            class(Entity), pointer, intent(out) :: c
            type(Aircraft), target, intent(in) :: t

            c => t

        end subroutine allocatePointer

    End Module Initialization

This above just example code written on a computer that doesn't have a compiler. I did my best and hopefully if there are typos they are few. I did my best to mirror the structure of the original code.

The problem is that the field "objects%trajTable" goes back to a undefined pointer after it leaves the "objectsInit" subroutine. The other values like time, id, and currPos are still correct. How can I correct this?

I am using Visual Studio 2012 and Intel Visual Fortran 2015.

Here is a link to the stack overflow question which has slightly better formatting. http://stackoverflow.com/questions/31439117/allocatable-array-of-inherit...


Viewing all articles
Browse latest Browse all 1424

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>