2

I have a procedure pointer that I need to pass on a few functions down and it crashes when compiling with gfortran (but not with ifort). Here is a minimal example to demonstrate the problem:

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

Obviously in my program, my version of "printme2" does a little more than just that, but you get the point. It calls another routine lots of times and passes on the procedure pointer every single time. Now with the Intel compiler this works as expected:

$ ifort segfault.f90 && ./a.out
1.00000000000000        4.00000000000000        9.00000000000000

However, with gfortran (v4.4.5-8):

$ gfortran segfault.f90 && ./a.out
Segmentation fault

Note that it works if I replace printme2 with printme in the test program. Why does this happen? What am I doing wrong and how do I make it right?

4
  • 3
    I don't have GCC 4.4.5 around but your code compiles and works fine with gfortran 4.5.3, 4.6.1 and 4.7.0 and doesn't compile at all with 4.3.4. I would say you hit a bug or something not implemented right in 4.4.5. You can peek at the assembly output but you'd better just upgrade the compiler. Jul 23, 2012 at 10:48
  • 1
    Alright, I'll ask our admin for an upgrade. Thanks for testing!
    – Volker
    Jul 23, 2012 at 10:55
  • 4
    Note that allowing internal procedures as the proc-target of a procedure pointer assignment is a feature from the Fortran 2008 standard.
    – IanH
    Jul 23, 2012 at 11:55
  • 1
    I tested also, it works fine with Gfortran 4.6.3 which is a stable version. Jul 25, 2012 at 13:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.