私はgfortran 7.2を使ってこのコードをUbuntu 16.04LTSでコンパイルしようとしています。Fortranでネストされた派生型の割り当て
コンパイルで警告が表示されますWarning: ‘unknowns.28.var’ may be used uninitialized in this function [-Wmaybe-uninitialized]
。
サンプル を実行しようとすると、このステートメントallocate(x%p(i)%vars(kount),stat=ierr)
を参照してSegmentation fault - invalid memory reference.
を参照してください。
module fmod
implicit none
type unknowns
character , allocatable :: var
integer , allocatable :: exponent
end type unknowns
type term
real , allocatable ::coeff
integer, allocatable :: nounknowns
type(unknowns) , allocatable :: vars(:)
end type
type poly
integer , allocatable :: noterms
integer , allocatable :: nounknowns
integer , allocatable :: mdegree
type(term) , allocatable :: p(:)
end type poly
save
contains
subroutine Allocate_polynomial(x,noofterms)
type(poly) , allocatable:: x
integer noofterms
integer countterms
integer i
integer j
integer kount
integer ierr
countterms = noofterms
allocate(x,stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate polynomial structure : error code=', ierr
stop
end if
allocate(x%mdegree,stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate mdegree : error code=', ierr
stop
end if
allocate(x%nounknowns,stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate nounknowns : error code=', ierr
stop
end if
allocate(x%noterms,stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate noterms : error code=', ierr
stop
end if
allocate(x%p(countterms),stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate array P : error code=', ierr
stop
end if
kount = 10
do i = 1, countterms
allocate(x%p(i)%vars(kount),stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate P(',I,').vars(10) : error code=', ierr
stop
end if
allocate(x%p(i)%coeff,stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate P(',I,').coeff : error code=', ierr
stop
end if
allocate(x%p(i)%nounknowns,stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate P(',I,').nounknowns : error code=', ierr
stop
end if
do j=1,kount
allocate(x%p(i)%vars(j)%var,stat=ierr)
x%p(i)%vars(j)%var = ' '
allocate(x%p(i)%vars(j)%exponent,stat=ierr)
IF (ierr/=0) THEN
write(*,*) ' Could not allocate P(',I,').vars(',j,').exponent : error code=', ierr
stop
end if
x%p(i)%vars(j)%exponent = 0
end do
end do
end subroutine
subroutine DeAllocate_Polynomial(x,noofterms)
type(poly) , allocatable :: x
integer noofterms
integer i
integer j
do i = 1, noofterms
do j=1,10
deallocate(x%p(i)%vars(j)%var)
deallocate(x%p(i)%vars(j)%exponent)
end do
deallocate(x%p(i)%coeff)
deallocate(x%p(i)%vars)
deallocate(x%p(i)%nounknowns)
end do
deallocate(x%p)
deallocate(x%noterms)
deallocate(x%nounknowns)
deallocate(x%mdegree)
deallocate(x)
end subroutine
END MODULE fmod
Program PolyAlgebra
use fmod
implicit none
type(poly) , allocatable :: x
integer noofterms
integer ierr
noofterms = 5
call allocate_polynomial(x,noofterms)
call DeAllocate_polynomial(x,noofterms)
END
ようこそ走ったことを。すべてのFortranの質問については、tag [tag:fortran]を使用してください。あなたが使用した特定のバージョンタグを見る人はほとんどいません。バージョン固有の質問については、バージョンタグを追加することができますが、私の意見ではこの質問には必要ありません。 –
ifort 16は動作しますが、gfortはありません – IgorM
コンパイラの問題ですか? –