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

dynamic allocated arrays as subroutine arguments

mcalindan
Beginner
831 Views
Hi,

I recentlyexperimented memory corruption problems when passing dynamic allocate arrays to subroutines which expected static allocated ones. Icould not reproducethem on small examples but I am posting asmall program with such an approach. Do you see anypotential harmful code bellow, regarding the call to test_dyn subroutine?

Thanks,
Calin

! test_dyn_alloc.f90

!*********************************
program test_dyn_alloc
implicit none
real, allocatable :: mat(:,:)
integer mydim,i

mydim = 100
allocate(mat(mydim,mydim))
do i=1,mydim
call test_dyn(mydim,mat(1,i))
enddo

end program test_dyn_alloc
!*********************************

!*********************************
subroutine test_dyn(size,vect)
implicit none
integer size,i
real vect(1)

do i=1,size
vect(i)=i
enddo

end subroutine test_dyn
!*********************************
0 Kudos
1 Solution
Steven_L_Intel1
Employee
831 Views

I'm not sure what you mean by "expected static". There's no problem with what you've done here - an allocatable array is usable just like any other array variable. The only thing that can get you into trouble is if the called routine accepts the array as assumed shape with DIMENSION(:) and you don't have an explicit interface. That can happen for any kind of array argument.

The only gripe I have with your code is that you declare the dummy argument to be dimension (1) instead of (*).

View solution in original post

0 Kudos
2 Replies
Steven_L_Intel1
Employee
832 Views

I'm not sure what you mean by "expected static". There's no problem with what you've done here - an allocatable array is usable just like any other array variable. The only thing that can get you into trouble is if the called routine accepts the array as assumed shape with DIMENSION(:) and you don't have an explicit interface. That can happen for any kind of array argument.

The only gripe I have with your code is that you declare the dummy argument to be dimension (1) instead of (*).
0 Kudos
mcalindan
Beginner
831 Views
Thanks Steve.
0 Kudos
Reply