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

Getting the size of an allocatable array in a subroutine

ferrad01
Beginner
582 Views
How do get the size of an array passed into a subroutine? (The code below won't compile)

program sizeofff
real*8, allocatable :: fred(:)
integer :: siz

read(5,*) siz
allocate(fred(siz))
call fred1(fred)

end

subroutine fred1(fred)
real*8 :: fred(*)
write(6,*) size(fred)
return
end

0 Kudos
2 Replies
Arjen_Markus
Honored Contributor II
582 Views
Use something like:

module freddy

contains

subroutine fred1(fred)

real*8 :: fred(:)
write(6,*) size(fred)
return
end subroutine
end module freddy

program sizeofff
real*8, allocatable :: fred(:)
integer :: siz
read(5,*) siz
allocate(fred(siz))
call fred1(fred)
end

You need to specify an assumed-shape array - which is achieved with the colon (:).
But then the compiler needs to see the interface. Using a module is the easiest way
to do that.

Regards,

Arjen

0 Kudos
Steven_L_Intel1
Employee
582 Views
Note that SIZE returns the number of elements.
0 Kudos
Reply