我有一个程序指针,我需要传递几个函数,并在使用gfortran编译时崩溃(但不能使用ifort) . 以下是演示此问题的最小示例:
module mod1
abstract interface
function f(x)
double precision f
double precision, intent(in) :: x
end function f
end interface
contains
subroutine printme(g)
procedure(f), pointer, intent(in) :: g
write(*,*) g(1d0), g(2d0), g(3d0)
end subroutine printme
subroutine printme2(g)
procedure(f), pointer, intent(in) :: g
call printme(g)
end subroutine printme2
end module mod1
program test
use mod1
procedure(f), pointer :: pg
pg => g
call printme2(pg)
contains
function g(x)
double precision g
double precision, intent(in) :: x
g = x**2
return
end function g
end program test
显然在我的程序中,我的“printme2”版本不仅仅是这个,但你明白了 . 它多次调用另一个例程,并且每次都传递过程指针 . 现在使用英特尔编译器,这可以按预期工作:
$ ifort segfault.f90 && ./a.out
1.00000000000000 4.00000000000000 9.00000000000000
但是,使用gfortran(v4.4.5-8):
$ gfortran segfault.f90 && ./a.out
Segmentation fault
请注意,如果我在测试程序中将 printme2
替换为 printme
,则它可以正常工作 . 为什么会这样?我做错了什么,我该如何做对?