我有一個關於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