Allocatable arrays

A deferred-shape array that has the ALLOCATABLE attribute is referred to as an allocatable array. The bounds and shape of the array are determined when you perform one of the following tasks:
  • Allocate storage using an ALLOCATE statement
  • Perform argument association

Example

The following example declares an allocatable array and determines its bounds.

INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: arr
ALLOCATE(arr(10, -4:5, 20))   ! Bounds of arr are now defined (1:10, -4:5, 1:20)
DEALLOCATE(a)
ALLOCATE(arr(5, 5, 5))        ! Change the bounds of arr

If you compile your program with -qinitalloc, all elements of the allocatable array arr are initialized to zero.

Migration Tip:

If you do not know the size of an array at compile time, you can avoid unnecessary memory usage by making the array allocatable instead of declaring it with a maximum size.

FORTRAN 77 source
      INTEGER A(1000),B(1000),C(1000)
C 1000 is the maximum size
      WRITE (6,*) "Enter the size of the arrays:"
      READ (5,*) N
              ⋮
      DO I=1,N
        A(I)=B(I)+C(I)
      END DO
      END
Source for Fortran 90 or above:
INTEGER, ALLOCATABLE, DIMENSION(:) :: A,B,C
WRITE (6,*) "Enter the size of the arrays:"
READ (5,*) N
ALLOCATE (A(N),B(N),C(N))
   ⋮
A=B+C
END


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us