Allocatables in derived types and functions



Dear all,

I am using derived types. To retrieve content from a derived type I
usually write a function rather than using the %-symbol. I reckon that
I can use the following function to extract 'i' from an object (assume
that i is properly allocated and set):

type my_obj
integer,dimension(:),allocatable :: i
end type my_obj

contains

!----------------------------------------------------------------------

function get_i(this) result(res)

type (my_obj),intent(in) :: this
integer,dimension(:),allocatable :: res

allocate(res(size(this%i)))
res = this%i

end function get_i

!----------------------------------------------------------------------

Now I was wondering if I am allowed to use this function as an argument
in a function call?
For example:

call write_i(get_i(object))

!----------------------------------------------------------------------

subroutine write_i(i)

integer,dimension(:),intent(in) :: i

write(*,*) i

end subroutine write_i

!----------------------------------------------------------------------

If I try this, it compiles and gives a the right results. However, in a
slightly more elaborate program it segfaults exactly at the write
statement. So, I was wondering if I am allowed to use these kind of
constructs. If so, I am happy to post the piece of code that crashes
(also not too many lines).

I am using the intel compiler (build 20060706). All seems to work with
g95.

Thanks,

Arno

.



Relevant Pages

  • Derived types with pointer arrays. Functions to extract data
    ... Do no use functions that return pointers, ... end type obj ... subroutine new ... derived types containing arrays of derived types etc. ...
    (comp.lang.fortran)
  • Re: Subroutine question
    ... derived types will reduce the number of arguments that must be passed. ... > pasting the code twice instead of passing it on to a subroutine. ...
    (comp.lang.fortran)