我正在處理一個項目(這是明天到期的:/),並且當我試圖在自己寫的函數上使用單純形算法時遇到問題。它沒有工作,經過5個小時的搜索和實驗後,我發現了以下內容:FORTRAN:分段錯誤,當將數組參數傳遞給子例程
當我將函數傳遞給子例程時,此函數不能有任何數組參數。這是真的?因爲在我應該使用它的代碼中顯然應該工作。
我正在使用ifort編譯器。請參閱下面最少例如,我basicaly從http://malayamaarutham.blogspot.de/2006/02/passing-function-names-as-arguments-in.html
! Author : Kamaraju S Kusumanchi
! Email : [email protected]
! Last edited : Sun Feb 5 2006
!
! Sample program demonstrating the use of external attribute. This program
! shows how to pass function names as arguments in Fortran 90 programs.
!
! Compilation and execution steps
! $gfortran passing_functions.f90 -o passing_functions
! $./passing_functions
! beta = 5.500000
! beta = 1.500000
!
! I would appreciate any comments, feedback, criticism, mistakes, errors etc.,
! (however minor they are)
!
module dummy
implicit none
contains
!------------------------------------------------------------------------------
function func1(a)
implicit none
real :: a
real :: func1
func1 = a+5
end function func1
!------------------------------------------------------------------------------
function func2(b)
implicit none
real :: b(:)
real :: func2
func2 = b(1)
end function func2
!------------------------------------------------------------------------------
function func3(dyn_func, c)
implicit none
real :: c
real, external :: dyn_func
real :: func3
func3 = dyn_func(c)
end function func3
!------------------------------------------------------------------------------
function func4(dyn_func, c)
implicit none
real :: c(*)
real, external :: dyn_func
real :: func4
func4 = dyn_func(c)
end function func4
end module dummy
!------------------------------------------------------------------------------
program passing_functions
use dummy
implicit none
real :: alpha=0.5, beta
real :: gamma(2) = (/10,20/)
beta = func3(func1, alpha)
write(*,*) 'beta = ', beta
beta = func4(func2, gamma)
write(*,*) 'beta = ', beta
end program passing_functions
這花是輸出:
zeus$ passing.out
beta = 5.500000
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
passing.out 0000000000402D44 Unknown Unknown Unknown
passing.out 0000000000402C7C Unknown Unknown Unknown
libc.so.6 00002AFF7915D23D Unknown Unknown Unknown
passing.out 0000000000402B79 Unknown Unknown Unknown
zeus$
這SOLN可以解決這個問題。 http://stackoverflow.com/questions/2521072/function-pointer-arrays-in-fortran。我認爲問題是func4 doensnt知道dyn_func採取什麼參數。 – yosukesabai 2013-03-14 00:09:56