I would like to create a derived type containing allocatable character array components. However, when I try to allocate memory in subroutines, nothing happens. It may be more clear with the code example below:
program test
type t1
character(len=:), allocatable :: c(:)
end type t1
type(t1) :: t
call test_string1()
call test_string2(t)
contains
subroutine test_string1()
character(len=:), allocatable :: c(:)
allocate( character(10) :: c(1) )
write(*, *) 'Size in string1: ', len(c)
end subroutine test_string1
subroutine test_string2(this)
class(t1) :: this
allocate( character(10) :: this%c(1) )
write(*, *) 'Size in string2: ', len(this%c)
end subroutine test_string2
end program test
I expect that the output of such a code would be:
Size in string1: 10
Size in string2: 10
However, what I actually get is the following:
Size in string1: 10
Size in string2: 0
Therefore, the second subroutine allocates nothing for the t1%c... What am I doing wrong here ?
I have compiled the code with:
gfortran -c test.f08
gfortran -o test test.o
and the version of gfortran is the following:
$ gfortran -v
...
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)