PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

Free OpenACC Webinar

Allocatable derived types with allocatable members in F95

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
Maarten



Joined: 11 Aug 2007
Posts: 12

PostPosted: Tue May 24, 2011 1:54 pm    Post subject: Allocatable derived types with allocatable members in F95 Reply with quote

Dear all,

I hope this is right place to post this question.
I would like to know if it is possible to define allocatable derived types with allocatable members.
I give an example in the code below. I would expect that this would not work because at the time of the first allocate() call it is not yet known how much memory is needed, since the size of member is not yet known. Nevertheless, this seems to work in PGI VF (F95). But I'm afraid that there may be an out-of-bounds problem here, that's going unnoticed.

If this is correct programming then my next question is: can one do this with deeper levels of nesting, e.g. allocatable derived types with allocable derived-type-members, with allocatable members, etc.

Thanks in advance.

cheers,
Maarten



Code:

program prog
implicit none

integer :: i
type test_type
  real, allocatable :: member(:)
end type
      
type(test_type), allocatable :: test(:)
      
allocate(test(10))
do i=1,10
  allocate(test(i)%member(10))
end do
end program prog
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6213
Location: The Portland Group Inc.

PostPosted: Tue May 24, 2011 2:27 pm    Post subject: Reply with quote

Hi Maarten,

Quote:
I would like to know if it is possible to define allocatable derived types with allocatable members.
Sure. This was added as part of the F95 standard.

Quote:
If this is correct programming then my next question is: can one do this with deeper levels of nesting, e.g. allocatable derived types with allocable derived-type-members, with allocatable members, etc.
I've forgotten if this if F95 of F2003, but this should be fine as well.

Hope this helps,
Mat
Back to top
View user's profile
Maarten



Joined: 11 Aug 2007
Posts: 12

PostPosted: Wed May 25, 2011 7:12 am    Post subject: Reply with quote

Hi,

thanks for the fast answer, Mat!

Concerning my second question (nesting of allocatable derived types): since this is what I plan to do for my code I just want to be sure that that I made clear what I have in mind.
I've included some example code below. This compiles and runs fine with my PG VF (F95). Is this correct programming?

Thanks,
Maarten

Code:

program prog

   implicit none
   integer :: n_lvl1 = 3;
   integer :: n_lvl2 = 3;
   integer :: n_lvl3 = 3;
   integer :: i, j

   type test_lvl2_type
      real, allocatable :: test_lvl3(:)
   end type

   type test_lvl1_type
      type(test_lvl2_type), allocatable :: test_lvl2(:)
   end type
   
   type(test_lvl1_type), allocatable :: test_lvl1(:)

   allocate(test_lvl1(n_lvl1))
   do i=1,n_lvl1
      allocate(test_lvl1(i)%test_lvl2(n_lvl2))
      do j=1,n_lvl2
         allocate(test_lvl1(i)%test_lvl2(j)%test_lvl3(n_lvl3))
         test_lvl1(i)%test_lvl2(j)%test_lvl3(:) = j
         print *, test_lvl1(i)%test_lvl2(j)%test_lvl3
      end do
   end do

end program prog
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6213
Location: The Portland Group Inc.

PostPosted: Wed May 25, 2011 8:12 am    Post subject: Reply with quote

Looks fine to me. Probably should deallocate your memory in the same way using do loops, but it's not an issue in this small example.

- Mat
Back to top
View user's profile
Maarten



Joined: 11 Aug 2007
Posts: 12

PostPosted: Wed May 25, 2011 9:01 am    Post subject: Reply with quote

Thanks!
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
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 © phpBB Group