In the following program, an allocatable variable x is passed to a subroutine test_sub without being allocated. The corresponding allocatable dummy variable x_sub is allocated in test_sub:
module test_mod
implicit none
contains
subroutine test_sub(x_sub)
implicit none
real, allocatable :: x_sub(:)
! Execution !
! return
allocate(x_sub(1))
! deallocate(x_sub)
end subroutine test_sub
end module test_mod
program test
use test_mod, only: test_sub
implicit none
real, allocatable :: x(:)
! Execution !
print*, 'Before call to sub', allocated(x)
call test_sub(x)
print*, 'After call to sub', allocated(x)
end program test
When control is returned to the main program, x is allocated:
Before call to sub F
After call to sub T
If test_sub returns before allocating x_sub, or if x_sub is deallocated before returning, x is not allocated:
Before call to sub F
After call to sub F
I observe this behavior using both gfortran 4.4.7 and ifort 19.0.
My question is whether this is standard behavior. I would have expected to get a segfault when trying to pass x without it being allocated. I'm worried this behavior might lead to unexpected results in larger programs.