1
votes

Using Intel Visual Fortran XE 2013 (with 2003 syntax enabled) and Visual Studio 2012

I have a derived type with an allocatable array as a component. When I try to allocate an the array or set the allocatable array equal to an array, it doesn't seem to work. There is no error, but the values don't seem to be present.

Below is an example that should illustrate the issue.

Type myType
    integer :: var1, var2
    real*8,dimension(:), allocatable :: listOfReals
    integer, dimension(:), allocatable :: listOfInts
end Type myType

function setTheValues(someParams) result(typeA)

    type(params_T), intent(in) :: someParams
    type(myType_T) :: typeA

    type(lists_t)  :: lists 

    typeA%var1 = 1
    typeA%var2 = 2
    !where getList is a function that returns an 
    !object with an array of real numbers and an array of ints
    lists = getLists(someParams)
    typeA%listOfReals = lists%reals
    typeA%listOfInts = lists%ints

end function setTheValues 

this doesn't work. neither does:

function setTheValues(someParams) result(typeA)

    type(params_T), intent(in) :: someParams
    type(myType_T) :: typeA

    type(lists_t)  :: lists


    typeA%var1 = 1
    typeA%var2 = 2

    allocate(typeA%listOfReals(12))
    allocate(typeA%listOfInts(12))

    !where getList is a function that returns an 
    !derivedtype with an array of real numbers and an array of ints both of size 12
    lists = getLists(someParams)
    typeA%listOfReals = lists%reals
    typeA%listOfInts = lists%ints

end function setTheValues

I either get something saying the values are not present, it won't show me any values, or it gives me an array with what appear to be enormous random index values with bad data.

Any help would be greatly appreciated.

EDIT: I am doing similar things in other parts of the code and they seem to be working right even though the debugger still seems to be having problems displaying the values right.

1
For the first case, do you assume realloc_lhs? Beyond that, can you give a more complete example, in particular, what says "the values are not present"? - francescalus
I'm sorry I don't know what assume "realloc_lhs" is. The VS2012 debugger says undefined address for the value with a big X. - QuantumDebris
A compiler option (although I'm not familiar with VS): without it set (and the default is not) automatic (re-)allocation doesn't happen. Your first example (at least) is relying on this F2003 feature. - francescalus
From within Visual Studio, the way to enable the reallocate on assignment language feature (/assume:realloc_lhs is the equivalent command line option for this) is to set the project property Fortran > Language > Enable F2003 semantics to "yes". This changes some things other than just reallocation on assignment. - IanH
I have F2003 semantics set to yes as my question said. - QuantumDebris

1 Answers

1
votes
module MyStuff

use, intrinsic :: ISO_FORTRAN_ENV

implicit none

type myType
    integer :: var1, var2
    real (real64), dimension(:), allocatable :: listOfReals
end Type myType

contains

function setTheValues () result(typeA)
   type (myType) :: typeA

   typeA % var1 = 1
   typeA % var2 = 2

   allocate ( typeA % listOfReals(12))
   typeA % listOfReals = getList()

end function setTheValues

function getList ()

   real (real64), dimension (12) :: getList
   getList = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]

end function getList

end module MyStuff


program MyProg

use MyStuff

implicit none

type (myType) :: myType1

myType1 = setTheValues ()

write (*, *) myType1 % var1, myType1 % var2
write (*, *) myType1 % ListofReals

end program MyProg