I have a simple question regarding derived types and allocated arrays. Suppose we have defined the type
type demo_type
real(kind=8),allocatable :: a(:)
end type demo_type
and the function
function demo_fill(n) result(b)
integer, intent(in) :: n
real(kind=8) :: b(n)
b = 1.d0
end function demo_fill
Is it correct to write in the main program
type(demo_type) :: DT
DT%a = demo_fill(3)
Or is it necessary to first allocate DT%a to prevent accidentally overwriting other variables in memory?
type(demo_type) :: DT
allocate(DT%a(3))
DT%a = demo_fill(3)
They both compile, but I am wondering which is the correct way. Thanks!