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

alocatable arrays

steve_konarski
Beginner
397 Views
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
0 Kudos
1 Solution
Arjen_Markus
Honored Contributor I
397 Views
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

View solution in original post

0 Kudos
2 Replies
Arjen_Markus
Honored Contributor I
398 Views
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
0 Kudos
Les_Neilson
Valued Contributor II
397 Views
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
0 Kudos
Reply