The following code contains a derive type PER_T which contains a pointer to an array of derived type GRP_T.
The derived type GRP_T in turn contains a pointer to an array of derived type EL_T.
When the line marked (A) is executed, the members of pers(1) lose their values and are set to zero. The allocate on this line is not working; this is indicated by the pointer in line (B) which becomes NULL.
I have tracked this down to be due to the use of null() to initialise the els(:) pointer in line (C). If I remove this initialisation, the code works correctly.
Please can this be fixed?
Regards David
program anon
type el_t
real :: a
end type el_t
type grp_t
type(el_t), pointer :: els(:) => null() ! (C)
end type grp_t
type per_t
integer :: num
real :: x
type(grp_t), pointer :: grps(:) => null()
end type per_t
type(per_t), allocatable :: pers(:)
integer :: i
type(grp_t), pointer :: grps_ptr(:)
allocate (pers(1))
! Setup values of pers(1)
pers(1)%num = 1
pers(1)%x = 1.0
! Allocate grps(:) part of pers(1) to point to array of size 5
allocate(pers(1)%grps(5)) !< (A) BUG HERE. Value of pers(1)%num and pers(1)%x get changed to 0
grps_ptr => pers(1)%grps(:) !< (B) BUG HERE. grps_ptr is NULL POINTER and shouldn't be
! Allocate els(:) to point to array of size 1
do i=1, 5
allocate (pers(1)%grps(i)%els(1))
end do
! Set value of a in els
pers(1)%grps(1)%els(1)%a = 2.0
print *, pers(1)%num
print *, pers(1)%x
end program anon