|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: Nürnberg, Germany
|
Posted: Tue Feb 19, 2008 1:10 pm Post subject: ALLOCATABLE is invalid in the definition of a derived TYPE |
|
|
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
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 |
|
|
Robert
Joined: 29 Nov 2006 Posts: 445 Location: Manchester
|
Posted: Tue Feb 19, 2008 3:59 pm Post subject: |
|
|
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 |
|
|
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: Nürnberg, Germany
|
Posted: Tue Feb 19, 2008 4:50 pm Post subject: |
|
|
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 |
|
|
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: Nürnberg, Germany
|
Posted: Wed Feb 20, 2008 7:57 am Post subject: |
|
|
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 |
|
|
|
|
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
|