public
Created

Array resizing

  • Download Gist
resize.f90
FORTRAN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
PROGRAM test1
 
IMPLICIT NONE
 
INTEGER, DIMENSION(:,:), ALLOCATABLE :: a
INTEGER :: i,j,ni,nj
 
ni = 4
nj = 4
 
ALLOCATE(a(ni,nj))
 
DO i=1,ni
DO j=1,nj
a(i,j) = i * j
END DO
END DO
 
WRITE(*,*) UBOUND(a,1), UBOUND(a,2)
 
CALL reallocate(a,5,5)
 
!WRITE(*,*) a
 
END PROGRAM test1
 
SUBROUTINE reallocate(a,ni_new,nj_new)
IMPLICIT NONE
 
INTEGER,DIMENSION(:,:),ALLOCATABLE,INTENT(INOUT) :: a
INTEGER,DIMENSION(:,:),ALLOCATABLE :: temp
INTEGER,INTENT(IN) :: ni_new,nj_new
INTEGER :: ni_old,nj_old,i,j
 
ni_old = UBOUND(a,1)
nj_old = UBOUND(a,2)
 
ALLOCATE(temp(ni_new,nj_new))
 
WRITE(*,*) ni_old, nj_old
temp(1:ni_old,1:nj_old) = a
 
CALL MOVE_ALLOC(temp,a)
 
END SUBROUTINE reallocate

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.