2

I have a program that associates a pointer to an instance of a derived type, then checks whether the pointer is associated with the intended object. When I compile this with gfortran (9.2.0), the associated() function returns true as expected. However, when I compile the same program with ifort (19.0.4.243), the associated() function returns false.

Here is the program in question:

module mod_pointer_association

  type mytype
  end type mytype

contains

  subroutine test_association(obj)

    class(mytype), target, intent(in)::obj
    class(mytype), pointer::ptr

    ptr=>obj

    if (.not. associated(ptr)) then
       print *, "ptr not associated"
       error stop
    end if

    if (.not. associated(ptr, obj)) then
       print *, "ptr not associated with obj"
       error stop
    end if

  end subroutine test_association

end module mod_pointer_association

program test_pointer_assocation

  use mod_pointer_association

  type(mytype), target::myobj

  call test_association(myobj)

end program test_pointer_assocation

gfortran produces no output, as expected. ifort produces the following output:

$ ./test_pointer_association 
 ptr not associated with obj
jhaiduce
  • 408
  • 4
  • 13
  • 1
    I'm guessing that your are getting caught out by `associated(x,y)` being supposed to return false when `y` has zero storage size. This is explained in answer to the linked question. If that isn't what you are after, please [edit] to provide more detail on what you want to know. – francescalus Aug 11 '23 at 20:28
  • That was exactly the problem; ifort apparently considers a type instance without any members as having zero storage size. Adding a member variable to mytype caused ifort to generate an executable that behaves the way I expected. – jhaiduce Aug 11 '23 at 23:54

0 Answers0