私の Fortran コードでは、複数レベルの割り当てでジャグ配列を使用したいと考えています。私が意味するコードの例は
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
メモリを節約するために、このプログラミング戦略を実装したいと考えています。この例で保存されたメモリがそれほど大きくないことはわかっていますが、実際のプロジェクトでは、はるかに大きなデータ構造を扱っています。このプログラミング手法には、データが連続して格納されない、メモリ リークが発生しやすいなどの危険性があるかどうか疑問に思っていました。それとも、これは多くの欠点を伴わずにメモリを節約する便利な方法ですか? ありがとう。