Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

polym. array of polym. elements having array-valued type-bound func.

Ferdinand_T_
New Contributor II
405 Views

Hello,

with a program similair to the following example I ran into a very confusing problem:

A polymorphic array of pointers (containers with a reference "ptr" pointing to a polymorphic "obj"-type) is allocated and initialized. Then, calling a type-bound array valued function from any referenced "obj" in that array behind the first causes a segmentation fault, unless the array elements are accessed in a loop with index starting by 1.

More details are given below the code.

[fortran]

module m

 
    ! has array valued method
    type :: obj
        integer :: not_empty    ! avoid problems with empty types
    contains
        procedure :: array_fun
    end type

 

    ! reference container
    type :: obj_container
        integer :: not_empty    ! avoid problems with empty types
        CLASS(obj), pointer :: ptr                          ! (*)
    end type

 

contains
    FUNCTION array_fun(this)
        implicit none
        class(obj) :: this
        integer, DIMENSION(1) :: array_fun                  ! (**)
        array_fun = 1
    end function
end module

 

program p
    use m
    implicit none
    CLASS(obj_container), DIMENSION(:), allocatable :: my_containers ! (*)
    type(obj), target :: my_obj
    integer :: ii

 

    ! allocate containers
    allocate(my_containers(99))

 

    ! loop through containers and execute array_fun
    do ii = 2,99                                            ! (***)
        my_containers(ii)%ptr => my_obj

 

        print *, my_obj%array_fun()                         ! 1 (ok)
        print *, associated(my_containers(ii)%ptr, my_obj)  ! T (ok)
        print *, my_containers(ii)%ptr%array_fun()          ! SEGFAULT (not so ok)
    end do
end program

[/fortran]

The program terminates with a segmentation fault only if all of the following conditions are met:

  • Both the actual containers and it's contained elements are polymorphic via CLASS attributes (*)
    (Making the actual targeted "my_obj" polymorphic too doesn't affect the outcome)
  • Type-bound method is a FUNCTION which returns an ARRAY (**)
    (The use of a subroutine with intent(inout) array seems to work)
  • Container-ARRAY is used for "my_containers", dimension > 1
  • Accessing NOT the first element in the container-array or accessing elements in a loop NOT starting by 1 (***)
    (However, accessing all elements in a loop starting from 1 with any step (e.g. "do ii = 1,99,4") works)

That in the example all containers are initialized to the same target seams not to be the source of the error. A separate initialisation of the whole "my_containers" array to all different "obj" targets doesn't solve the porblem.

Thanks for any advice or fixes!

Best regards,

Ferdinand

0 Kudos
2 Replies
Ferdinand_T_
New Contributor II
405 Views

Just checked on this problem with the latest ifort update 13.1.3 and the problem disappeared.

0 Kudos
Steven_L_Intel1
Employee
405 Views

Glad to hear it.

0 Kudos
Reply