- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, you can't do it exactly, but you can emulate it pretty well:
Jugoslav
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 = 1Got the idea?
Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In current CVF, po can be ALLOCATABLE too if desired.
Steve
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Steve

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page