2017-12-13 8 views
2

私は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 
+0

ようこそ走ったことを。すべてのFortranの質問については、tag [tag:fortran]を使用してください。あなたが使用した特定のバージョンタグを見る人はほとんどいません。バージョン固有の質問については、バージョンタグを追加することができますが、私の意見ではこの質問には必要ありません。 –

+0

ifort 16は動作しますが、gfortはありません – IgorM

+0

コンパイラの問題ですか? –

答えて

2

gfortranのバグのように聞こえますが、問題は割り当て可能な文字を含むある種のフォームオブジェクトの割り当てになります。次のコードは、セグメンテーション違反でクラッシュ:

program test 
implicit none 
type my 
    character, allocatable :: var 
end type my 
type(my) , allocatable :: x 

allocate(x) 

end 

あなたの代わりにポインタを使用する場合、それは動作します:

:16のifort

program test 
implicit none 
type my 
    character, pointer :: var 
end type my 
type(my) , allocatable :: x 

allocate(x) 

end 

はこの問題に

EDITを持っていません遅延長さの文字変数を割り当てることもgfortran 7.2でも可能です(バージョンアップではサポートされていません〜4.9):

program test 
implicit none 
type my 
    character(:), allocatable :: var 
end type my 
type(my) , allocatable :: x 

allocate(x) 

end 
+0

あなたはどのバージョンをテストしましたか?割り当て可能な文字とコンポーネントには複数の問題がありました。また、無制限の多型の文字も使用できます。 –

+0

7.2私が試した最高のバージョンです。 – IgorM

+0

私はこのバージョンで最初のコードがクラッシュするかどうか分かりません。 –

1

ありがとうございます。
私は以下のように変更することによって、作業それを得た:

type unknowns 
      character(:) , allocatable :: var 
      integer , allocatable :: exponent 
end type unknowns 

allocate(character(len=1)::x%p(i)%vars(j)%var,stat=ierr) 

とコンパイルされ、成功し

関連する問題