Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29280 Discussions

Crash if temporary implicit pointer is created

Marat_S_1
Beginner
357 Views

Hi, 

Could you please explain the crash in the following program?

[fortran]

module A_m
  implicit none
  private
  
  type, public :: A_t
    private
    character(len=1)  :: dummy_char
  contains
    procedure :: DoSmth
  end type

contains
  subroutine DoSmth(this)
    class(A_t), intent(in) :: this
    write(*, "(A)") "A_t::DoSmth"
  end subroutine DoSmth

end module A_m


module B_m
  use A_m
  implicit none
  private

  type, public :: B_t
    private
    class(A_t), pointer :: a => null()
  contains
    procedure :: GetA
    procedure :: SetA
  end type

contains
  subroutine SetA(this, a)
    class(B_t), intent(inout)         :: this
    class(A_t), pointer, intent(in)   :: a
    this%a => a
  end subroutine SetA

  function GetA(this) result(a)
    class(B_t), target, intent(in) :: this
    class(A_t), pointer            :: a
    a => this%a
  end function GetA

end module B_m


program main
  use A_m
  use B_m
  implicit none
  class(A_t), pointer :: a
  class(B_t), pointer :: b
  class(A_t), pointer :: a_ptr

  allocate(a, b)
  call b%SetA(a)

  ! works with explicit pointer
  a_ptr => b%GetA()
  call DoA(a_ptr)

  ! crashes here
  call DoA(b%GetA())

  deallocate(a, b)

contains
  subroutine DoA(a)
    class(A_t), pointer, intent(in) :: a
    if (.not.associated(a)) then
      write(*, "(A)") "error"
    else
      call a%DoSmth()
    end if
  end subroutine DoA

end program main

[/fortran]

I believe that the program crashes because an implicit pointer is created at the statement "call DoA(b%GetA())".

Compiler version: XE 13.1

0 Kudos
1 Reply
Steven_L_Intel1
Employee
357 Views

I am looking at this now.

0 Kudos
Reply