【发布时间】:2013-05-01 09:10:11
【问题描述】:
我有一个关于 Fortran 和正确分配的问题 可分配的用户派生类型。
这是我的代码:
module polynom_mod
implicit none
type monomial
integer,dimension(2) :: exponent
end type
type polynom
real, allocatable, dimension(:) :: coeff
type(monomial),allocatable, dimension(:) :: monom
logical :: allocated
!recursive type
type(polynom),pointer :: p_dx,p_dy
contains
procedure :: init
procedure :: init_dx
end type
在这里我想导出一个类型多项式,我可以在其中执行以下操作:
p%coeff(1)=1.0
p%monom(1)%exponent(1)=2
类似的东西:
p%p_dx%coeff(1)=1.0
p%p_dx%monom(1)%exponent(1)=2
所以我写了一些初始化类型绑定的过程,我可以在其中初始化和分配我的 类型:
contains
function init(this,num) result(stat)
implicit none
integer, intent(in) :: num
class(polynom),intent(inout) :: this
logical :: stat
allocate(this%coeff(num))
allocate(this%monom(num))
this%allocated = .TRUE.
stat = .TRUE.
end function
function init_dx(this,num) result(stat)
implicit none
integer, intent(in) :: num
class(polynom),intent(inout) :: this
logical :: stat
allocate(this%p_dx%coeff(num))
allocate(this%p_dx%monom(num))
this%p_dx%allocated = .TRUE.
stat = .TRUE.
end function
end module
program testpolytype
use polynom_mod
type(polynom) :: p
if(p%init(2)) then
print *,"Polynom allocated!"
end if
if(p%p_dx%init_dx(2)) then
print *,"Polynom_dx allocated!"
end if
结束程序
这将使用 gfortran 4.6.3 编译,但是当我运行它时,我遇到了分段错误!
有没有办法分配递归可分配类型?
【问题讨论】:
标签: types fortran allocation