forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Use of TYPE arrays

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Fri Sep 18, 2009 4:25 am    Post subject: Use of TYPE arrays Reply with quote

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 ?

Code:
!     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
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7926
Location: Salford, UK

PostPosted: Fri Sep 18, 2009 8:26 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group