问题
In my Fortran code, I want to use jagged arrays with multiple levels of allocation. An example code of what I mean is
module nonsquare_matrix_mod
implicit none
type :: nonsquare_matrix
integer :: d
real*8, dimension(:), allocatable :: vector
end type nonsquare_matrix
type(nonsquare_matrix),dimension(:),allocatable :: mymatrix
end module nonsquare_matrix_mod
program nonsquare_matrix_test
use nonsquare_matrix_mod
implicit none
integer, parameter :: max_size=50
integer :: i
allocate(mymatrix(max_size))
do i=1,max_size
allocate(mymatrix(i) % vector(i))
end do
print *, "allocated"
end program
I want to implement this programming strategy in order to conserve memory. I know the memory saved in this example isn't too large, but for my actual project, I'm working with much larger data structures. I was wondering if there are any dangers with this programming practice, such as data not being stored contiguously or being more prone to memory leaks. Or is this a useful way of saving memory without many drawbacks? Thanks.
回答1:
Since you are using allocatable arrays only, you won't have any memory leak as it could be the case when using pointers. Whether the jagged array is a reasonable solution for your problem depends very much on the structure of your data. A few points to be noted:
Your array won't be contigous, indeed. That has several implication, like worse caching behaviour, when you are accessing subsequent lines.
You have to allocate every line via
allocate
individually. If it happens very often (e.g. inside a loop), that could be a problem, as allocation is a rather "slow" operation.If the lines in your array are really very different in size (and you do not have too many lines), you may save some considerable amount of memory.
If the length of the lines is set at the time of their creation and not changed afterwards (and you have a good guess of maximally how much elements you will have in your entire array), you could allocate a large buffer array, where you put in the lines, and an index array which contains the position of the first element of that line in the buffer array:
program nonsquare_matrix_test
implicit none
integer, parameter :: dp = kind(1.0d0)
integer, parameter :: maxlines = 50
integer, parameter :: maxelements = 5000
real(dp), allocatable :: buffer(:)
integer, allocatable :: rowindex(:)
integer :: ii
allocate(buffer(maxelements))
allocate(rowindex(maxlines + 1))
rowindex(1) = 1
do ii = 1, maxlines
rowindex(ii + 1) = rowindex(ii) + ii
end do
! ...
! Omitting the part which fills up the array
! ...
! Accessing a given line (e.g. line 5)
print *, "Content of line 5:"
print *, buffer(rowindex(5):rowindex(6)-1)
end program nonsquare_matrix_test
来源:https://stackoverflow.com/questions/14857366/are-there-any-problems-with-using-jagged-arrays-in-fortran-with-multiple-levels