Silverfrost Forums

Welcome to our forums

Use of TYPE arrays

18 Sep 2009 3:25 #4975

Paul,

Following on from the question I asked previously, the following example appears to show that the compiler generates a temporary array, when providing part of a Type data set. Presumably the temporary array is on the stack ? With large type data sets, could this lead to stack overflow problems ? Or have I mis-understood my example ?

!     Last change:  JDC  18 Sep 2009    9:03 am
module my_data
integer*4, parameter :: mxnode = 10

TYPE Coordinate_Record 
   real*8 X 
   real*8 Y 
   real*8 Z 
   integer*4 fixity(6) 
END TYPE Coordinate_Record 

type (Coordinate_record) nodes(mxnode)

end module

use my_data
integer*4 index(mxnode), i

do i = 1,mxnode
   call random_number (nodes(i)%x)
   call random_number (nodes(i)%y)
   call random_number (nodes(i)%z)
end do

call dsort@ (index, nodes%z, mxnode)  ! this works

do i = 1,mxnode
   write (*,'(2i5,3f10.6)') i, index(i), nodes(index(i))%x, nodes(index(i))%y, nodes(index(i))%z
end do

call test_argument_1 (nodes%z, mxnode)   ! change is lost
write (*,1000) 'type',nodes(10)%z, ' change to nodes(i)%z lost'

call test_argument_2 (nodes%z, mxnode)   ! change is updated
write (*,1000) 'type',nodes(10)%z, ' change to nodes_z(i) updated'

1000 format (a,f10.6,a)

end

subroutine test_argument_1 (nodes_z, num)

use my_data

real*8 nodes_z(num)
integer*4 num, i

   i = 10
   write (*,1000) 'type',nodes(i)%z, ': Argument',nodes_z(i)
   nodes(i)%z = i
   write (*,1000) 'type',nodes(i)%z, ': Argument',nodes_z(i)
   return
1000 format (2(a,f10.6))
end

subroutine test_argument_2 (nodes_z, num)

use my_data

real*8 nodes_z(num)
integer*4 num, i

   i = 10
   write (*,1000) 'type',nodes(i)%z, ': Argument',nodes_z(i)
   nodes_z(i) = i
   write (*,1000) 'type',nodes(i)%z, ': Argument',nodes_z(i)
   return
1000 format (2(a,f10.6))
end
18 Sep 2009 7:26 #4976

I don't have a compiler to hand to test this out but the behaviour might vary depending on the INTENT state of the argument. If the state is INTENT(IN) then it is sometimes better to say so.

Please login to reply.