Well basically you have fixed (static) arrays which are defined e.g. using dimension
:
real,dimension(4) :: X
X
is an array of length 4
(1
-4
). This is equivalent to:
real :: X(4)
Static arrays have a fixed length throughout their scope (e.g. throughout the program for global variables or throughout functions/subroutines).
What you need are allocatable
arrays which are allocate
d at runtime:
program test
implicit none
real, allocatable :: B(:) ! The shape is given by ":" - 1 dimension
integer :: stat
! allocate memory, four elements:
allocate( B(4), stat=stat )
! *Always* check the return value
if ( stat /= 0 ) stop 'Cannot allocate memory'
! ... Do stuff
! Clean up
deallocate( B )
! Allocate again using a different length:
allocate( B(3), stat=stat )
! *Always* check the return value
if ( stat /= 0 ) stop 'Cannot allocate memory'
! No need to deallocate at the end of the program!
end program