This example shows how to dynamically allocate and deallocate memory using pointers.
fortranCopy codeprogram dynamic_memory
implicit none
integer, pointer :: ptr(:)
integer :: n, i
n = 10
allocate(ptr(n))
! Initialize array
do i = 1, n
ptr(i) = i * 2
end do
! Print array elements
print *, 'Dynamic array elements:'
do i = 1, n
print *, ptr(i)
end do
! Deallocate array
deallocate(ptr)
end program dynamic_memory
Dynamic Memory Allocation with Pointers