Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29375 Discussions

Procedure pointer as function result ?

gregory_lantoine
Beginner
505 Views
Hello all,

I'm using Visual Fortran 11 on Windows 32 and I found a problem when I'm trying to use a procedure pointer as a result of a function. I'm getting the error 'Program Exception - access violation'. Here is the code:

[cpp]
PROGRAM MAIN
use TestMod
implicit none
type(object_ptproc) ptproc
external DummyFunc

    ptproc = test_ptproc(DummyFunc)
    call ptproc%pt(2)

END PROGRAM MAIN

subroutine DummyFunc(n)
implicit none
integer, intent(in) :: n
print*, n
end subroutine DummyFunc
[/cpp]
module TestMod abstract interface subroutine OutputFunc(n) integer, intent(in):: n end subroutine OutputFunc end interface type object_ptproc procedure(OutputFunc), nopass, pointer :: pt => NULL() end type object_ptproc CONTAINS function test_ptproc(OutputFcn) result(ptproc) implicit none type(object_ptproc) :: ptproc external OutputFcn ptproc%pt => OutputFcn end function test_ptproc end module TestMod


However, when I use a subroutine instead of a function, it's working fine :

[cpp]PROGRAM MAIN
use TestMod
implicit none
type(object_ptproc) ptproc
external DummyFunc

    call test_ptproc(ptproc,DummyFunc)
    call ptproc%pt(2)

END PROGRAM MAIN

subroutine DummyFunc(n)
implicit none
integer, intent(in) :: n
print*, n
end subroutine DummyFunc

module TestMod

        abstract interface
	    subroutine OutputFunc(n)
	        integer, intent(in):: n
	    end subroutine OutputFunc
	end interface
	
	type object_ptproc
           procedure(OutputFunc), nopass, pointer :: pt => NULL()
	end type object_ptproc
	
	CONTAINS
	
	subroutine test_ptproc(ptproc,OutputFcn) 
	implicit none
	type(object_ptproc), intent(out) :: ptproc
	external OutputFcn
	ptproc%pt => OutputFcn
	end subroutine test_ptproc
	
end module TestMod[/cpp]

Do you think this is a bug of Visual Fortran ? If so, can it be corrected ?

Thanks a lot in advance !

Regards,

Gregory
0 Kudos
2 Replies
gregory_lantoine
Beginner
505 Views
Sorry, there was a problem when I copy-pasted the first code. Here is the right one:

PROGRAM MAIN
use TestMod
implicit none
type(object_ptproc) ptproc
external DummyFunc

ptproc = test_ptproc(DummyFunc)
call ptproc%pt(2)

END PROGRAM MAIN

subroutine DummyFunc(n)
implicit none
integer, intent(in) :: n
print*, n
end subroutine DummyFunc

module TestMod

abstract interface
subroutine OutputFunc(n)
integer, intent(in):: n
end subroutine OutputFunc
end interface

type object_ptproc
procedure(OutputFunc), nopass, pointer :: pt => NULL()
end type object_ptproc

CONTAINS

function test_ptproc(OutputFcn) result(ptproc)
implicit none
type(object_ptproc) :: ptproc
external OutputFcn
ptproc%pt => OutputFcn
end function test_ptproc

end module TestMod
0 Kudos
Steven_L_Intel1
Employee
505 Views
It's a compiler bug. In version 11.0, test_ptproc is not returning the pointer correctly. This is fixed in version 11.1, coming out in a few months.
0 Kudos
Reply