I have a question about Fortran and correct allocation of allocatable user derived types.
Here is my code:
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
here I want to derive a type polynom where I can do things like:
p%coeff(1)=1.0
p%monom(1)%exponent(1)=2
and something like:
p%p_dx%coeff(1)=1.0
p%p_dx%monom(1)%exponent(1)=2
so I wrote some init type-bound procedures where I can initialize my and allocate my types:
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
end program
This will compile with gfortran 4.6.3 but when I ran it I got a segmentation fault!
Is there a way to allocate recursive allocatable types?