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.

Array of pointers

thlund
Beginner
1,340 Views
Hi

I wish to have an allocatable array of pointers, but
i seem to have run foul in the declaration.

So.... what do i mean ?

With a simple pointer
"type (bla_bla), pointer :: po"
i can access the variables in the type bla_bla with the %

But now i wish to have an array of pointers that i can
allocate to my wishes. So i was thinking :
"type (bla_bla), pointer :: po(:)"
"allocate( po(1:10) )"
but then the size of po is 10 times the size of the
bla_bla structure and not 10 times the size of a pointer.

What do i do ? Can i do it at all ?

Thanks
Thomas
0 Kudos
8 Replies
Jugoslav_Dujic
Valued Contributor II
1,340 Views
Well, you can't do it exactly, but you can emulate it pretty well:
type p_BlaBla
   type(bla_bla), pointer:: po => NULL()
end type p_BlaBla

type(p_blabla), allocatable:: pbb
allocate(pbb(10))
allocate(pbb(4)%po)
pbb(4)%po%foo = 1
Got the idea?

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
1,340 Views
In current CVF, po can be ALLOCATABLE too if desired.

Steve
0 Kudos
Intel_C_Intel
Employee
1,340 Views
Steve,
Could you please amplify on this a little?
In C, arrays are vectors of vectors, allowing non-uniform column length. This is particularly useful for upper-right or symmetric matrix storage. It's also nice for building collections of data to be passed through argument lists.
I didn't think this was possible in Fortran. Jugoslav's construct of a pointer type is very clever and I may use it, but if you can give an example that doesn't require this abstraction, it might be more transparent to others reading my code in later years.

Specifically, can you do something like;

Integer (4), Target :: a, b
Integer (4), Pointer :: c(:)
c(1) => a
c(2) => b

This generates a compiler error, reinforcing my belief that you can declare a pointer to an array, but not an array of pointers.

Any insight you can offer will be appreciated.
Cliff
0 Kudos
Steven_L_Intel1
Employee
1,340 Views
What I said doesn't negate what Jugoslav said - Fortran does not allow you to directly declare an array of pointers. You have to use a derived type that has a POINTER component, or if it works for you, you can now have an ALLOCATABLE component. The difference is that an ALLOCATABLE array is always contiguous (a POINTER can refer to a discontiguous section) and assignment of derived types with ALLOCATABLE components automatically does deallocation, reallocation and copying of data as needed.

You can certainly have an array of derived types with ALLOCATABLE array components, each with its own length and allocation status.

Keep in mind that a Fortran pointer, unlike a C pointer, is more than just an address - it is a descriptor with base address, rank, bounds and stride information.

Steve
0 Kudos
thlund
Beginner
1,340 Views
Thanks, but i did think of this. In the program i am
doing i am getting some very long %%%% expressions,
so what i was looking for was a way to cut down on
the lengths.

This certainly is the second best after what i was
looking for.

Thanks
Thomas
0 Kudos
durisinm
Novice
1,340 Views
Steve,

Can you expand on your comment about Fortran pointers? I knew that Fortran pointers aren't the same as C pointers; you can't, for instance, have a Fortran pointer to a function. Your comment about a Fortran pointer being a descriptor with a base address, rank, bounds, and stride information is the first time I can remember seeing that kind of description. Does the CVF documentation contain a more thorough discussion of this?

Can you point me (no pun intended) toward a table that compares and contrasts the characteristics of C and Fortran pointers?

Mike
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,340 Views
Mike, look for "Handling Arrays and Visual Fortran Array Descriptors" page in Mixed-Language chapter of Programmer's Guide -- it's rather thorough.

Regarding pointers to functions, I think it's an ommission in F95; I guess that, even if it was raised on J3 Standards Commitee at the time, they didn't have time to work out all necessary details. This is (supposed to be, didn't look at the details yet) fixed in Fortran 2000. (http://www.j3-fortran.org/)

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
1,340 Views
Let me clarify. POINTER to array is a descriptor. POINTER to a scalar is just an address. This is the F90 POINTER, not the "Cray POINTER" extension. This is discussed in various places in the Programmer's Guide, especially in the mixed-language chapter.

Steve
0 Kudos
Reply