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 

ALLOCATABLE is invalid in the definition of a derived TYPE

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



Joined: 21 Jun 2006
Posts: 404
Location: Nürnberg, Germany

PostPosted: Tue Feb 19, 2008 1:10 pm    Post subject: ALLOCATABLE is invalid in the definition of a derived TYPE Reply with quote

Hi,

I have a derived type which should have an 2-dimensional array as a component. However, the use of allocatable is not allowed in a derived type. The solution is to use a pointer.

The basic problem is as follows: I have to read a entry from a database and keep all the data as a single variable in the program. Along with the 2-dimensional data there are some header information.

Below is some example code from what I am trying to do. I think that there must be some better way of assiging the 2D data than below.

And that better way is what I am looking for Smile I would appreciate any help, since this a problem that I often have.

Regards
Jacques

Code:

program pointers
  implicit none
  integer :: i,j,ii

  type main_type
    integer :: n
    real,dimension(:,:),pointer :: x
  end type

  real,allocatable,dimension(:,:) :: old,var,new

  type(main_type) :: main

  allocate(new(1,10))
  do i=1,10 
    allocate(old(i,10))
    do j = 1,10
      new(i,j) = j
    enddo
    old = new

    if (i==10) then;exit;endif
    deallocate(new)
   
    ! increase size
    allocate(new(i+1),10)
    new = 0.0
    do ii=1,i
      new(ii,1:10)=old(ii,1:10)
    enddo       
    deallocate(old)
  enddo

  main%x => new
  deallocate(old)
  deallocate(new)

end program pointers
Back to top
View user's profile Send private message
Robert



Joined: 29 Nov 2006
Posts: 445
Location: Manchester

PostPosted: Tue Feb 19, 2008 3:59 pm    Post subject: Reply with quote

Not sure what to suggest really. If you have, say a 10x20 array that you want to fill with values, the chances are you are going to have two nested loops somewhere that set the data values.
Back to top
View user's profile Send private message Visit poster's website
jjgermis



Joined: 21 Jun 2006
Posts: 404
Location: Nürnberg, Germany

PostPosted: Tue Feb 19, 2008 4:50 pm    Post subject: Reply with quote

Hi Robert,

I think the general question is how to read data from a file from which the size is unknown - without making use of static arrays. One option is as follows:
1. first read to the end of the file to determine the size
2. allocate the desired array
3. rewind the file and read data into the array

This is sort of the old way. Instead it is possible to dynamically allocate the variable while read the data or even the use of pointers.

The priciple is known, with some simple example I would be able to figure it out myself.

Regards
Jacques
Back to top
View user's profile Send private message
jjgermis



Joined: 21 Jun 2006
Posts: 404
Location: Nürnberg, Germany

PostPosted: Wed Feb 20, 2008 7:57 am    Post subject: Reply with quote

Hi,

below is some code which I figured out. It works. The solution avoids allocatable variables in a derived type by making use of a pointer.

Jacques

Code:

program pointers
  implicit none
  integer :: i,j,newsize

  type main_type
    integer :: n
    real,dimension(:,:),pointer :: dvar =>null()
  end type

  real,dimension(:,:),pointer :: newvar =>null()
 
  type(main_type) :: main

  ! Get the first row of data
  allocate(main%dvar(1,10))
  main%dvar(1,1:10) = (/(i,i=1,10)/)

  ! A new row is available - assign
  do i=2,10 
    newsize = size(main%dvar,1) + 1
    allocate(newvar(newsize,10))
    newvar(1:newsize-1,:) = main%dvar
    deallocate(main%dvar)
    main%dvar => newvar
    do j = 1,10
      main%dvar(i,j) = j
    enddo
  enddo
  deallocate(newvar)

  ! Print data to screen
  do i=1,10 
    write(*,'(10F5.1)') (main%dvar(i,j),j=1,10)
  enddo 

end program pointers
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support 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