You are not logged in.

#1 2013-02-11 08:55:33

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

[SOLVED] Fortran: derived type with array of pointers to that type

I have a derived type, and I want it to contain an array of pointers to the same type. To make an array of pointers, I have to define another derived type. I've tried it like this:

type mytype
   ...
   type(p2mytype),allocatable,dimension(:) :: p
   ...
end type mytype

type p2mytype
   type(mytype),pointer :: p
end type p2mytype

The problem is that this causes circular dependencies as both type contain each other, i.e. regardless of which type definition comes first, one of them is used before it is defined. Is there a work-around?

Last edited by cryptkeeper (2013-02-15 05:31:43)

Offline

#2 2013-02-13 00:25:26

WesleyDimble
Member
Registered: 2012-09-03
Posts: 18

Re: [SOLVED] Fortran: derived type with array of pointers to that type

Hiya,

I really don't know what I'm doing when messing around with Fortran, but this compiled and spat something out at me:

module foo                                                                                                                                                                                                         
    implicit none
    type :: p2mytype
        type(mytype),pointer :: p
    end type p2mytype
    type :: mytype
        type(p2mytype),allocatable,dimension(:) :: p
        integer :: i
    end type mytype
end module foo 
 
program testfoo
    use foo 
    implicit none
    type(mytype),target :: mts(10)
    integer      :: i
 
    do i = 1, 10
        mts(i)%i = i 
    end do
 
    allocate(mts(1)%p(10))
 
    do i = 1, 10
        mts(1)%p(i)%p => mts(11-i)
    end do
 
    do i = 1, 10
        print *, mts(1)%p(i)%p%i
    end do
 
end program testfoo

So.. maybe try moving the p2mytype declaration before the mytype one again? You said that doing so didn't work for you, but it seems to be needed for this to compile for me.

Last edited by WesleyDimble (2013-02-13 00:27:29)

Offline

#3 2013-02-13 23:41:43

bsilbaugh
Member
From: Maryland, USA
Registered: 2011-11-15
Posts: 141

Re: [SOLVED] Fortran: derived type with array of pointers to that type

Off the top of my head, you might also consider using a pointer to an array of mytype instead of an array of pointers to mytype:

type mytype
...
type(mytype), dimension(:), pointer :: p
...
end type mytype

It's simpler and more readable. However, it is not strictly equivalent to your original data structure. For example, with an array of pointers you can dynamically redefine the collection of mytype's referenced by another mytype (e.g. swap out the ith mytype with another); whereas with a pointer to an array of mytype, you can only redefine which collection is referenced by another mytype (e.g. swap out the entire array with another).

The only other recommendation I can think of is to stop using Fortran smile I waisted about 3 years of my scientific programming career wrestling with Fortran (95). Then I realized that, unless you're dealing with simple data structures (e.g. arrays), you're better off using a language like C or C++. (Right now I'm also exploring Haskell, but I don't have enough experience with Haskell yet to draw any kind of meaningful conclusions.) Just my two cents...


- Good judgement comes from experience; experience comes from bad judgement. -- Mark Twain
- There's a remedy for everything but death. -- The wise fool, Sancho Panza
- The purpose of a system is what it does. -- Anthony Stafford Beer

Offline

#4 2013-02-15 05:31:21

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

Re: [SOLVED] Fortran: derived type with array of pointers to that type

WesleyDimble wrote:

Hiya,
...
So.. maybe try moving the p2mytype declaration before the mytype one again? You said that doing so didn't work for you, but it seems to be needed for this to compile for me.

Thanks for the input, I've tried it once again and it actually works like this! I'm quite sure I've tried it this way; or actually I'm not so sure anymore now and feel kinda stupid for this... But I've been messing around with the code for quite some time and it never worked. So far, it at least compiled, so I guess it'll work.

bsilbaugh wrote:

Off the top of my head, you might also consider using a pointer to an array of mytype instead of an array of pointers to mytype:
... (e.g. swap out the ith mytype with another) ...

That is actually what I do, i.e. sort the array of pointers by swapping the links. That's what I like so much about working with pointers, that you can easily re-link them.

The only other recommendation I can think of is to stop using Fortran smile I waisted about 3 years of my scientific programming career wrestling with Fortran (95). Then I realized that, unless you're dealing with simple data structures (e.g. arrays), you're better off using a language like C or C++. .

At least now, that's not really an option (everyone else uses it), and I don't really want to drop Fortran, anyway. I've been using it daily for only half a year and quite like it. But I'm aware of certain advantages of C++, as I've learned it first and used it for like a year to mess around with some modeling stuff (nothing too serious, as I'm only just about to finish my Master's anyway) before switching to Fortran (for the Master's thesis). Out-of-the-box, I find Fortran more convenient to work with physical data because of the simple array operations (i.e. you don't have to make huge loop structures for every simple array addition etc.), though of course defining a class allowing for such direct array operations in C++ would not be too difficult.

But I think I'll at least have a look at newer Fortran (i.e. 2003) and it's OOP stuff.

Thanks you two for your answers!

Offline

Board footer

Powered by FluxBB