2012-08-09 55 views
0

我在Fortran中存在分段錯誤問題。 我通過調用一個子例程來分配一個指針數組,並將這個數組傳遞給另一個子例程。指針數組中的Fortran分段錯誤

我在Linux機器上用PGI fortran 9.0.2編譯這個程序。 這是我的測試程序。我應該如何解決這個問題?

非常感謝您的幫助。

module hogehoge 
    implicit none 
    type foo 
    real, pointer :: x(:)=>null() 
    end type foo 
contains 
    subroutine hogehoge_func(foox) 
    type(foo), intent(inout) :: foox 
    integer i 
    allocate(foox%x(2048)) 
    do i = 1, 2048 
     foox%x(i)=i 
    end do 
    end subroutine hogehoge_func 
end module hogehoge 

!main program------------------------------------------ 
program test 
    use hogehoge 
    implicit none 
    type(foo) :: foox 
    call hogehoge_func(foox) 

    print*, 'in main program' 
    print*, foox%x(1:20) 
    call hoge(foox%x) 

end program test 
subroutine hoge(foox) 
    use hogehoge 
    implicit none 
    type(foo), intent(in) :: foox 

    print*, 'in subroutine' 
    print*, foox%x(1) 

end subroutine hoge 

這是輸出。

in main program 
    1.000000  2.000000  3.000000  4.000000 
    5.000000  6.000000  7.000000  8.000000 
    9.000000  10.00000  11.00000  12.00000 
    13.00000  14.00000  15.00000  16.00000 
    17.00000  18.00000  19.00000  20.00000 
in subroutine 
Segmentation fault 
+1

DaveP在下面有一個正確的答案。另外,如果你使用-C編譯你的程序(檢查邊界),你會得到一個關於你的程序出了什麼問題的非常豐富的消息。 – milancurcic 2012-08-09 04:34:39

回答

7

您需要主程序的最後一行從改變:

call hoge(foox%x) 

call hoge(foox) 

如果你有一個模塊中定義的常規hoge和進口的,然後編譯器會選擇這種類型的錯誤。