Paul,
I tried to understand what was the problem with the derived type not being updated.
It appears that
- if the derived type is referenced it is recognised as to be updated following a call, but
- if the derived type is in scope (via a module) it is not recognised as to be updated following a call.
Was this the problem ?
Is this an optimisation effect ?
although I used plato with x64 and CheckMate
I am interested as I am using derived types in modules and in scope via use, but have not noticed this problem.
In scope via contains looks to be a different copy of the derived type ?
I also thought that 'print*, r(1)' was not allowed in F95, but 'print*, r(1)%val' would be required, although this also fails in a similar way.
The following extension of Ken's code reproduces the problem and shows if the derived type is an argument to a called routine, the problem does not appear.
module data_mod
implicit none
type winreal
sequence
integer :: n = 1.0
real :: val = 1.0
end type winreal
type(winreal) :: r(1)
contains
subroutine init (val)
real :: val
r(1)%n = r(1)%n + 1
r(1)%val = val ! 10.0
print*, r(1), ' init to 10'
end subroutine init
subroutine increase_r
r(1)%n = r(1)%n + 1
r(1)%val = r(1)%val * 10.0
print*, r(1),' increase_r by 10'
end subroutine increase_r
subroutine nl
write (*,*) '~'
end subroutine nl
end module data_mod
program main
use data_mod
integer :: i
call nl
print*, r(1) ! Value printed should be 1
do i = 1,3
call increase_r
print*, r(1), 'main DoR' ! Value printed should be 10,100,1000 since value changed by subroutine Increase_R
end do
print*, r(1)%val, 'main end DoR' ! Value printed should be 10,100,1000 since value changed by subroutine Increase_R
call nl
call init (2.0)
print*, r(1), 'main init' ! Value printed should be 10 since value changed by subroutine INIT
do i = 1,3
call increase_a (r)
print*, r(1), 'main DoA' ! Value printed should be 20,200,2000 since value changed by subroutine Increase_A
end do
call nl
call init (10.0)
print*, r(1), 'main init' ! Value printed should be 10 since value changed by subroutine INIT
r(1)%val = 100.0
print*, r(1), 'main 100' ! Value printed should now be 100
end program main
subroutine increase_a (a)
use data_mod
type(winreal) :: a(1)
write (*,*) ' ',a
a(1)%n = a(1)%n + 1
a(1)%val = a(1)%val * 10.0
print*, a(1),' increase_a by 10'
end subroutine increase_a
I notice that for my example a%n (and r%n) do not increment as I would hope. Is this a related problem or is this non-conforming use of a%n ?