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

Address of a pointer to a subroutine changes in a call to a subroutine

jtau
Beginner
361 Views
I need some help in understanding the following:

Assigning a pointer to a subroutine and looking at the address of the "pointee" in the debuggers watch list gives "a value" in the main program. While passing this pointee to another subroutine and assigning another pointer to this address shows a "different value" for its address. However when using this pointer to call the orginal subroutine the call is successful.

Here's an example ...
!***********************************
!***********************************
program PointerTest
implicit none

external ASub, BSub
integer :: BSubPtr
pointer (p, BSubPtr)

p = loc(BSub)
!Here loc(BSubPtr) == loc(BSub)
call ASub(BSubPtr)

end program
!***********************************
!***********************************
subroutine ASub(OldSubPtr)
integer :: OldSubPtr, NewSubPtr
pointer(p, NewSubPtr)

p = loc(OldSubPtr)
!Here loc(OldSubPtr) == loc(BSub) /= loc(NewSubPtr) Why?
call NewSubPtr()
!Note the call to BSub through NewSubPtr is successfull
!However, it seems like they should have the same
!address/location.

end subroutine
!***********************************
!***********************************
subroutine BSub()

end subroutine
!***********************************
!***********************************

Thanks for any help.
0 Kudos
3 Replies
Steven_L_Intel1
Employee
361 Views
Which compiler and version are you using?

The program you post here is not legal becasuse NewSubPtr needs to be declared EXTERNAL in Asub in order to call it. (That's the requirement we put on using an integer pointer to a routine.)

If I add the EXTERNAL declaration, then CVF 6.6C does what you describe - I'm not sure why. Intel Visual Fortran won't link the program - that's a bug I'll report.
0 Kudos
jtau
Beginner
361 Views
Thanks for the response.

I'm using CVF Professional 6.5.0.
One other question.
Why can I access "BSub" through through "NewSubPtr" when its address is different than "BSub". Does it really point to the correct address and for some reason "loc(NewSubPtr)" returns the wrong address.

Thanks again.
0 Kudos
Steven_L_Intel1
Employee
361 Views
I believe it's just the loc that is misbehaving. loc() requires a lot of special-case code in the compiler and has been a source of trouble in the past.
0 Kudos
Reply