next up previous contents
Next: Pointers and Procedures Up: Pointers and Targets Previous: Dynamic Targets   Contents

Pointers to Arrays vs Allocatable Arrays

We can create space dynamically using allocatable arrays, or using pointer allocation. In general, allocatable arrays are more efficient, while pointer allocation is more flexible.

There are two main restrictions imposed upon allocatable arrays which do not apply to pointer arrays:

For example,

program ptralloc
implicit none
real, dimension(:), allocatable :: a
real, dimension(:), pointer :: b
  allocate(a(4)) 
  call t1(a)
  print*, a
  call t2(b)
  print*, b
contains
  subroutine t1(a)
    real, dimension(:) :: a
    a = reshape((/1.,2.,3.,4./),(/4/))
  end subroutine t1
  subroutine t2(b)
    real, dimension(:), pointer :: b
    allocate(b(4));
    b = reshape((/5.,6.,7.,8./),(/4/))
  end subroutine t2
end program ptralloc



Adrian Sandu 2001-08-26