"spectrum" wrote in message
news:d4f3319e-aafa-41e5...@googlegroups.com...
> To get more info, I have tried a slightly modified code with
> gfortran-7.2 on OSX10.11, and it reproduced the same behavior
> (i.e., the "key" in the second object not defined). My test code looks
> like this:
[snip]
Thanks for further investigating and using valgrind. I wrote some
more test code on my own and found that the bug is really specific
which is why it wasn't detected before. I tried using a structure
constructor with inputs other that an allocatable-length component
and gfortran handled it OK. Even assigning allocatable component
to allocatable component was OK. Allocatable SIZE rather than LEN
was OK.
C:\>type bug4.f90
module mod1
implicit none
type s1
character(:), allocatable :: key
end type s1
type s2
character, allocatable :: key(:)
end type s2
contains
subroutine sub(x)
character(*) x
type(s1) z
z = s1(x)
write(*,'(*(g0))') 'ALLOCATED(z%key) = ',ALLOCATED(z%key)
write(*,'(*(g0))') 'LEN(z%key) = ',LEN(z%key)
write(*,'(*(g0))') 'z%key = ',z%key
end subroutine sub
end module mod1
program main
use mod1
implicit none
type(s1) x
type(s1) z
type(s1) w
character(:), allocatable :: u
type(s1) v
type(s2) a
type(s2) b
x = s1('Sojin')
write(*,'(*(g0))') 'ALLOCATED(x%key) = ',ALLOCATED(x%key)
write(*,'(*(g0))') 'LEN(x%key) = ',LEN(x%key)
write(*,'(*(g0))') 'x%key = ',x%key
x = s1('Yura')
z = s1(x%key)
write(*,'(*(g0))') 'ALLOCATED(z%key) = ',ALLOCATED(z%key)
write(*,'(*(g0))') 'LEN(z%key) = ',LEN(z%key)
write(*,'(*(g0))') 'z%key = ',z%key
x = s1('Minah')
w%key = x%key
write(*,'(*(g0))') 'ALLOCATED(w%key) = ',ALLOCATED(w%key)
write(*,'(*(g0))') 'LEN(w%key) = ',LEN(w%key)
write(*,'(*(g0))') 'w%key = ',w%key
call sub('Hyeri')
u = 'Jihae'
v = s1(u)
write(*,'(*(g0))') 'ALLOCATED(v%key) = ',ALLOCATED(v%key)
write(*,'(*(g0))') 'LEN(v%key) = ',LEN(v%key)
write(*,'(*(g0))') 'v%key = ',v%key
a = s2(transfer('Jiin',['x']))
write(*,'(*(g0))') 'ALLOCATED(a%key) = ',ALLOCATED(a%key)
write(*,'(*(g0))') 'SIZE(a%key) = ',SIZE(a%key)
write(*,'(*(g0))') 'a%key = ',a%key
a = s2(transfer('Jisun',['x']))
b = s2(a%key)
write(*,'(*(g0))') 'ALLOCATED(b%key) = ',ALLOCATED(b%key)
write(*,'(*(g0))') 'SIZE(b%key) = ',SIZE(b%key)
write(*,'(*(g0))') 'b%key = ',b%key
end program main
C:\>gfortran bug4.f90 -obug4
C:\>bug4
ALLOCATED(x%key) = T
LEN(x%key) = 5
x%key = Sojin
ALLOCATED(z%key) = T
LEN(z%key) = 0
z%key =
ALLOCATED(w%key) = T
LEN(w%key) = 5
w%key = Minah
ALLOCATED(z%key) = T
LEN(z%key) = 5
z%key = Hyeri
ALLOCATED(v%key) = T
LEN(v%key) = 5
v%key = Jihae
ALLOCATED(a%key) = T
SIZE(a%key) = 4
a%key = Jiin
ALLOCATED(b%key) = T
SIZE(b%key) = 5
b%key = Jisun