我在fortran中有一个派生数据类型,如下所示:
TYPE mytype
INTEGER a
DOUBLE COMPLEX, dimension(:), allocatable :: elem
END TYPE mytype现在,我想编写一个函数,它将一个双复数组作为参数。这个数组应该成为"mytype“的数组"elem”,而不需要分配内存和复制数据。我试图以以下方式使用一个cray指针:
DOUBLE COMPLEX, INTENT(IN) :: eleminput(5) !input array which should become x%elem
TYPE(mytype) :: x
pointer(xpntr,x%elem)
xpntr = LOC(eleminput)当我试图编译时,我会看到一个错误,在“指针(xpntr,x%elem)”行中,我说它是expacting ")“而不是"%”。因此,似乎疯狂的指针不适用于派生数据类型的元素。是否有可能使这一工作,或也没有愚蠢的指针?派生数据类型不能更改。我希望你不要回答我的问题,谢谢你的帮助。
发布于 2015-03-23 23:14:34
你可以移动一个分配。如果eleminput参数是可分配的:
integer, parameter :: dp = kind(1.0d0)
type mytype
integer :: a
complex(kind=dp), dimension(:), allocatable :: elem
end type
...
subroutine foo(eleminput)
complex(kind=dp), intent(inout), allocatable :: eleminput(:)
type(mytype) :: x
call move_alloc(eleminput, x%elem)
!... work with x
end subroutine foo与可分配的虚拟参数相关联的实际参数本身必须是可分配的--也就是说,对foo的调用必须类似于:
complex(kind=dp), allocatable :: fred(:)
fred = [(0,0),(0,1),(1,1),(1,0)]
call foo(fred)因为分配是从子例程eleminput中的虚拟参数fred中移出的,所以当该子例程返回时,实际的参数fred将被取消分配。
发布于 2015-03-23 21:56:54
Cray指针不适用于可分配对象。我强烈建议不要在新代码中使用Cray指针,因为Cray指针在实现细节上有细微差别,而普通的Fortran指针在这里工作:
module bar
integer, parameter :: dp = selected_real_kind(15)
TYPE mytype
INTEGER:: a
complex(kind=dp), dimension(:), allocatable :: elem
END TYPE mytype
contains
subroutine foo(eleminput)
complex(kind=dp), dimension(:), intent(out), pointer :: eleminput
type(mytype), target, save : : x
allocate (x%elem(.....))
eleminput => x%elem
end subroutine foo
end module barhttps://stackoverflow.com/questions/29220822
复制相似问题