- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thanks,
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
Link Copied
1 Reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
![](/skins/images/D2683F18326913BBA0436CB7114DD569/responsive_peak/images/icon_anonymous_message.png)
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page