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

problem with allocatable character variable inside a polymorphic linked list

Juan_Pablo_S_1
Beginner
783 Views

Hello. I noticed this problem with an allocatable character variable when used inside a polymorphic linked list compiled with  ifort version 13.0.1  . The output is the following. 

First output: allocatable character

D

D

D

D

Second output: assigned length character

A

B

C

D

What am I doing wrong?

[fortran] program main
implicit none
type :: linked_list
class(linked_list), pointer :: prev
class(linked_list), pointer :: next
class(*), pointer :: data
end type

type :: named_type_A
character(:),allocatable :: name
end type

type :: named_type_B
character(len=1) :: name
end type


type(named_type_A),target :: t_a
type(named_type_B),target :: t_b
class(*),pointer :: polydata

class(linked_list),pointer :: listA, listB
character(len=1),dimension(4) :: ch
integer :: i_list

ch=(/'A', 'B', 'C ', 'D'/)

! FIRST OUTPUT CORRECT
print*, 'First output: allocatable character'
polydata=>t_a

allocate(listA)

do i_list = 1, 4
t_a%name=ch(i_list)
allocate(listA%data,source=polydata)
allocate(listA%next)
listA%next%prev=>listA
listA=>listA%next
end do

do while(associated(listA%prev))
listA=>listA%prev
end do

do i_list = 1, 4
select type(q=>listA%data)
type is(named_type_A)
print*, q%name
end select
listA=>listA%next
end do

print*, 'Second output: assigned length character'
polydata=>t_b

allocate(listb)

do i_list = 1, 4
t_b%name=ch(i_list)
allocate(listb%data,source=polydata)
allocate(listb%next)
listb%next%prev=>listb
listb=>listb%next
end do

do while(associated(listB%prev))
listB=>listB%prev
end do

do i_list = 1, 4
select type(q=>listB%data)
type is(named_type_B)
print*, q%name
end select
listB=>listB%next
end do


end program[/fortran]

0 Kudos
5 Replies
Steven_L_Intel1
Employee
783 Views

I don't think you are doing anything wrong, but I need to investigate further. My guess is that this is a compiler bug,  I will let you know.

0 Kudos
Steven_L_Intel1
Employee
783 Views

I can reproduce this, though the synmptoms vary depending on how and on which platform I build it. I have escalated the problem as issue DPD200240765. Thanks for the nice example.

0 Kudos
Steven_L_Intel1
Employee
783 Views

We have fixed this for a release later this year.

0 Kudos
Steven_L_Intel1
Employee
783 Views

The developer who worked on this just sent me an additional note...

There is actually a fairly significant bug in this code.  In both case 1 and case 2, the first list element never nullifies its prev field, and then relies on that field being NULL later on in the code: 

[fortran]
do while(associated(listA%prev))
listA=>listA%prev
end do
[/fortran]

To fix this, after the line:   

[fortran]
allocate(listA)
[/fortran]

there needs to be added the line:   

[fortran]
nullify(listA%prev)
[/fortran]

and after the line:   

[fortran]
allocate(listB)
[/fortran]

there needs to be added the line:   

[fortran]
nullify(listB%prev)
[/fortran]

This is actually the cause of several of the manifestations of the problem reported in the original description.

0 Kudos
Juan_Pablo_S_1
Beginner
783 Views

Sorry for this unwanted bug. But in the original code in which I first noticed this problem,  the  line 4 of the example actually was

[fortran]

class(linked_list), pointer :: prev=>null()

[/fortran]

Thanks,

Juan

0 Kudos
Reply