Print

Print


Hi:

I'm debugging some truly insane code, have isolated an issue illustrated
by the following example.

        module a_mod

          type :: some_type
            integer :: some_stuff
          end type some_type

          type :: type1
            type(some_type) :: foo
          end type type1

          type :: type2
            type(some_type), pointer :: bar => null()
          end type type2

        contains

          function foo_ptr (t1)
            type(type1), intent(in), target :: t1
            type(some_type), pointer :: foo_ptr
            foo_ptr => t1%foo
          end function foo_ptr

          subroutine set_bar_ptr (t2, st)
            type(type2), intent(out) :: t2
            type(some_type), intent(in), target :: st
            t2%bar => st
          end subroutine set_bar_ptr

        end module a_mod

        program test_it
          use a_mod
          type(type1), target :: t1
          type(type2) :: t2
          call set_bar_ptr (t2, foo_ptr(t1))
          print *, associated(t2%bar, t1%foo)  ! TRUE OR FALSE?
        end program test_it

What value should the ASSOCIATED instrinsic return?  Of the compilers
I've tried, all return true except for one.  This latter one appears
to have passed a _copy_ of t1%foo to set_bar_ptr, and when this copy
is deallocated after returning, the pointer t2%bar is left dangling.
Indeed, is it illegal to pass an expression as the second argument to
set_bar_ptr (even when the result of that expression is a pointer)?
If I pass a variable, this works; or if I make the second dummy argument
of set_bar_ptr a pointer it works too.

Thoughts/comments?

Thanks!
  Neil
--
Neil Carlson <[log in to unmask]>