我有一个关于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编译但是当我跑了我有一个分割的错!
有没有办法分配递归可分配类型?
谢谢!对于导致分段错误的未初始化指针,你是正确的。 – 2013-05-08 10:47:29