Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

How to increase array size

Roberto_Soares
Beginner
334 Views
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 Kudos
1 Reply
Steven_L_Intel1
Employee
334 Views
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.
0 Kudos
Reply