Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29394 Discussions

Passing allocatable array to a subroutine

Alireza_Forghani
Beginner
926 Views

I'm trying to pass an allocatable array, which is already allocated, to a subroutine. something like this:

real(8), dimension(:,:), allocatable :: A

allocate (A (6,6))

call subroutine mysub(A)



subroutine mysub(B)
real (8), dimension (:,:) :: B

write (*,*) B(1,1)

This causes segmentation fault when mysub is accessing B. I have had the same approach to pass the array to a function and it worked fine. I wonder what's going wrong here. 

Thanks,

Alireza

0 Kudos
3 Replies
mecej4
Honored Contributor III
926 Views

If you call a subroutine whose arguments include allocatable or assumed-shape arrays, the interface to the subroutine must be provided in the caller. This can be done in a number of ways, and I suggest that you either create and use a module for this purpose or make the subroutine and its caller contained subroutines.

0 Kudos
Martyn_C_Intel
Employee
926 Views

You need to provide an explicit interface for subroutine B. Otherwise, the caller doesn't know it is supposed to provide all the array bounds information etc., as opposed to just a starting address. You can look up "Doctor Fortran" articles about this, for example, https://software.intel.com/en-us/blogs/2012/01/05/doctor-fortran-gets-explicit-again .

0 Kudos
Alireza_Forghani
Beginner
926 Views

Thanks very much. Now I understand why the function worked, but not the subroutine. The function was defined in a module.  

0 Kudos
Reply