- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you have an allocatable array say A and it is initially set to a size of 1000 and you want to increase to 2000, is there any way of keeping the contents of A for entries 1 to 1000 -while increasing the array size. At present the only way round it is to store the contents 1-1000 in a temporary array, delallocate A then put the contents of the temporary array back into the resized to array A nowhaving 2000 entries
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can improve on that scenario via the F2003 routine move_alloc:
integer, allocatable, dimension(:) :: a, tmp
allocate( a(1000) )
... initialise a ...
! Increase its sizevia move_alloc
allocate( tmp(2000) )
tmp(1:size(a)) = a
deallocate( a )
call move_alloc( tmp, a )
You still need the temporary array, but you only need one allocation
and one copy action.
Regards,
Arjen
integer, allocatable, dimension(:) :: a, tmp
allocate( a(1000) )
... initialise a ...
! Increase its sizevia move_alloc
allocate( tmp(2000) )
tmp(1:size(a)) = a
deallocate( a )
call move_alloc( tmp, a )
You still need the temporary array, but you only need one allocation
and one copy action.
Regards,
Arjen
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can improve on that scenario via the F2003 routine move_alloc:
integer, allocatable, dimension(:) :: a, tmp
allocate( a(1000) )
... initialise a ...
! Increase its sizevia move_alloc
allocate( tmp(2000) )
tmp(1:size(a)) = a
deallocate( a )
call move_alloc( tmp, a )
You still need the temporary array, but you only need one allocation
and one copy action.
Regards,
Arjen
integer, allocatable, dimension(:) :: a, tmp
allocate( a(1000) )
... initialise a ...
! Increase its sizevia move_alloc
allocate( tmp(2000) )
tmp(1:size(a)) = a
deallocate( a )
call move_alloc( tmp, a )
You still need the temporary array, but you only need one allocation
and one copy action.
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I think you don't even need to deallocate(a).
After the move_alloc you may wish toensure the new elements are initialised :
a(oldsize+1:) = some_initial_value
and for safety(say if you do a lot of re-allocating) you may wish to add some error checking.
Les
After the move_alloc you may wish toensure the new elements are initialised :
a(oldsize+1:) = some_initial_value
and for safety(say if you do a lot of re-allocating) you may wish to add some error checking.
Les
![](/skins/images/C4F7392A5C68ACA60290C9576F107044/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