Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29415 ディスカッション

How to increase array size

Roberto_Soares
ビギナー
770件の閲覧回数
Hi,
I am using Visual Fortran Compiler for Windows. My question is how to increase an array size, while keeping the previous values.
For example, if I have a(100) with 100 values stored, later I will need to increase a's size to 200, so a(200). However I would like to keep the previous 100 values and start populating with new values from 101...200.
Any suggestions?

Thanks,
Roberto
0 件の賞賛
1 返信
Steven_L_Intel1
従業員
770件の閲覧回数
You cannot "extend" an existing array, but you can create a new one with the old one's data. This is best done using ALLOCATABLE arrays and the MOVE_ALLOC intrinsic, which lets you do it with one copy instead of two. Something like this:

REAL, ALLOCATABLE :: A(:)
REAL, ALLOCATABLE :: AX(:) ! Extended copy of A
...
ALLOCATE (A(100))
... A now has 100 elements
ALLOCATE (AX(200)) ! Create a new array of the larger size
AX(1:100) = A ! Copy in existing elements
CALL MOVE_ALLOC (AX,A)
... A is now 1:200 and AX is deallocated

Of course, you may be able to avoid this by allocating the array to the needed larger size initially, but for cases where you need to "extend", the above is the way to do it in Fortran.
返信