Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

How to get a pointer from the address of it?

Zhanghong_T_
Novice
1,165 Views
I know we can get the address of a pointer by
type pp
intger i
end type
type(pp), pointer:: p
...
locp=loc(p)
But if we know locp, how can I get p?
Thanks,
Zhanghong,Tang
0 Kudos
11 Replies
Jugoslav_Dujic
Valued Contributor II
1,165 Views

In general, you can use Cray (integer) pointers, but they don't go along with normal Fortran pointers you have.

I use the following trick. It is based on CVF/IVF feature that a scalar pointer is the same as C pointer (a single address, i.e. void*). Thus, it's non-standard but a good thing is that it can be isolated in a separate source.

First, define a FUNCTION outside any module which just returns its argument passed by value:

function Cast(p)
!DEC$ATTRIBUTES VALUE:: p
integer(INT_PTR_SIZE()):: p
integer(INT_PTR_SIZE()):: Cast
Cast = p
end function Cast

Then, redefine an interface to "convince" the compiler that its return value is not an integer, but a pointer to something. You have to write one interface body for each type you want to cast:

interface
function CAST_PP(p)
!DEC$ATTRIBUTES DECORATE, ALIAS: "CAST":: CAST_PP
!DEC$ATTRIBUTES VALUE:: p
integer(int_ptr_size()), intent(in):: p
type(PP),pointer:: CAST_PP
endfunction
end interface

Finally, the usage is as simple as:

USE TheModuleWithInterface

type(pp), pointer:: p

p => CAST_PP(locp)

Note that this is basically a dirty trick, but it can get usefulon occasion:-) .

Jugoslav

0 Kudos
Zhanghong_T_
Novice
1,165 Views
Thank you very much!
Zhanghong, Tang
0 Kudos
Zhanghong_T_
Novice
1,165 Views

Another question: from your code I can get a known pointer from its address. But if I don't know what the structure of the pointer is, how can I get it? In other words, how can I get any kindof pointer from an address? just like

pointer(address, paddress)

I ask this question is because IVF8.0 don't support for a DEC Fortran pointer.
Sincerely,
Zhanghong Tang
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,165 Views
But if I don't know what the structure of the pointer is, how can I get it? In other words, how can I get any kindof pointer from an address?
Structure of pointer or pointee/target? You have to know the datatype of pointee/target, or otherwise you cannot do anything with it.
If you mean, structure of pointer itself, in C it's always just an address. CVF/IVF implementation differs for scalar and array pointers. Scalar pointers (to anything) is just an address (thus the trick above works). Array pointers are implemented as a structure (known as array descriptor or dope vector), whose size depends on the rank; for 1d arrays, the structure is 28 bytes long (for CVF, I suspect IVF changed it). It used to be described in "Handling arrays and Visual Fortran array descriptors" help page.
I ask this question is because IVF8.0 don't support for a DEC Fortran pointer.
That shouldn't be. Anything that works in CVF in this field should work in IVF as well. Cray (integer) pointers are supported.
Side note: Fortran 2003 draft, in C interop part, finally standardizes these issues. A C pointer (address) can be converted to Fortran pointer by an intrinsic called, IIRC, C_TO_F_PTR().
Jugoslav
0 Kudos
Steven_L_Intel1
Employee
1,165 Views
Yes, "DEC" (Cray) pointers are supported in IVF. Absolutely.
0 Kudos
Zhanghong_T_
Novice
1,165 Views
Thank you again: Steve and Jugoslav!
Here I pasted a code which can run well in CVF but can't run in IVF8.0.

module ppp
type node
type(node), pointer::next=>null()
endtype

type list
type(node), pointer::head=>null()
integer:: nodesize = 0
endtype
contains
subroutine pass( icurr, il)
integer icurr, ifl
type(node), pointer::curr
type(node), target:: T_curr
type(list), pointer::l
type(list), target:: T_l
pointer(icurr, T_curr)
pointer(il, T_l)
curr=>T_curr
l=>T_l
curr % next => l % head
l % head => curr
end subroutine
end module

use ppp
type(list), pointer::l
integer::t, tt
allocate(l)
l%nodesize=30
t=malloc(4 * l%nodesize)
do i=0,3
call pass(t+i*l%nodesize,loc(l))
enddo
end

Would you please point out the problems?

Sincerely,

Zhanghong Tang

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,165 Views

Without much involvement on what it does, I compiled & ran the codeon IVF8.

However, there's adebugger bug described here -- does it match your observations?

Jugoslav

0 Kudos
Zhanghong_T_
Novice
1,165 Views

Thank you very much! In this program It's my default.

But I have a real project which is large (runs successfully in CVF). When compile one of the files, it displays 'Compilation Aborted (code 1)' and doesn't display any other information about the errors. Howshould I do to find out the bugs?

PS: The code I pasted above is among the file which I think leading to the error.

Sincerely,
Zhanghong Tang
0 Kudos
Steven_L_Intel1
Employee
1,165 Views
Try compiling the source from the command line and see if you get the same result ("Compilation aborted" message and no other messages.) If you do, please send an example that demonstrates this to Intel Premier Support. (Before you do that, though, make sure you have the latest version from Premier Support, which is 8.0.047, I think.)
0 Kudos
Zhanghong_T_
Novice
1,165 Views
Thank you again!
I have tried from the command line, it displayed:
fortcom: Fatal: There has been an internal compiler error (C0000005).
compilation aborted for xx.f90 (code 1)
PS: I still use the trial version of IVF8.0 so I have not registed in Premier Support. In addition, I will try to give an example which can demonstrate this error.
Sincerely,
Zhanghong Tang
0 Kudos
Steven_L_Intel1
Employee
1,165 Views
You have access to Premier Support - register using the serial number you were sent. Then log on to Premier Support and you can download the current version, as long as it's within 30 days of the trial start.
0 Kudos
Reply