Silverfrost Forums

Welcome to our forums

ALLOCATABLE is invalid in the definition of a derived TYPE

19 Feb 2008 12:10 #2801

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 😃 I would appreciate any help, since this a problem that I often have.

Regards Jacques

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
19 Feb 2008 2:59 #2802

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.

19 Feb 2008 3:50 #2803

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

20 Feb 2008 6:57 #2804

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

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
Please login to reply.