[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode
Program Main
  Integer , allocatable :: x(:)
  Real    , allocatable :: y(:)
  allocate(x(4),y(4))
  x = 3 ; y = 4
  write(*,*) x
  write(*,*) y
  call ReallocateInt( x , 3 )
  call ReallocateReal( y , 6 )
  write(*,*) x
  write(*,*) y
  
contains
#define TYPE_  Integer  
  Subroutine ReallocateInt( x , n )
    TYPE_ , allocatable :: x(:) , t(:)
    integer :: n , s
    call move_alloc(x,t)
    allocate(x(n))
    s = min(size(t),n)
    x(:s) = t(:s)
    deallocate(t)
  End Subroutine ReallocateInt
#define TYPE_  Real  
  Subroutine ReallocateReal( x , n )
    TYPE_ , allocatable :: x(:) , t(:)
    integer :: n , s
    call move_alloc(x,t)
    allocate(x(n))
    s = min(size(t),n)
    x(:s) = t(:s)
    deallocate(t)
  End Subroutine ReallocateReal
  
End Program Main