assumed shape arrays, colon vs "*"

This little code,

program szarr
integer, dimension(10) :: bleh
bleh(1)=-5
call arrop(bleh)
end program


subroutine arrop(blah)
integer, intent(in) :: blah(*)
print *,blah(1)
end subroutine

works just fine and prints “-5” just like I expected. But, I make one little change in the subroutine:

integer, intent(in) :: blah(:)


and it dies with a seg fault.

Everything I have read says the colon should work just like the “*”. Heck, I even work with a code where this works. But it doesn’t work for my little test here. Can someone tell me what I am missing? Thanks.

Hi cablesb,

You need an interface for routines that pass in assumed shape arrays. Either add an explicit interface, or put arrop in a module.

For example:

% cat test2.f90 

program szarr
interface testme
  subroutine arrop(blah)
   integer, intent(in) :: blah(:)
  end subroutine arrop
end interface testme

integer, dimension(10) :: bleh
bleh(1)=-5
call arrop(bleh)
end program

subroutine arrop(blah)
integer, intent(in) :: blah(:)
print *,blah(1)
end subroutine 

% pgfortran test2.f90; a.out
           -5
%
% cat test3.f90
module testme

contains
subroutine arrop(blah)
integer, intent(in) :: blah(:)
print *,blah(1)
end subroutine 

end module testme

program szarr
use testme
integer, dimension(10) :: bleh
bleh(1)=-5
call arrop(bleh)
end program

% pgfortran test3.f90 ; a.out
           -5
%

Hope this helps,
Mat

Thanks, mkcolg!

It’s odd to me that I have looked up alot of material on this, but no one just comes out and says you have to have a module or interface. Do you know of where a good discussion might be on the whys and wherefores of this? Thanks again.

Hi Calblesb,

It’s because assumed shaped arrays need the F90 descriptor passed with it since the callee needs to know the shape at run time. Without an interface, the caller must use F77 calling conventions and only a pointer to the array is passed.

The requirement can be found in section 12.3.1.1. of the F2003 standard. Note 2b.

22 12.3.1.1 Explicit interface

23 A procedure other than a statement function shall have an explicit interface if it is referenced and

24 (1) A reference to the procedure appears
25 (a) With an argument keyword (12.4.1),
26 (b) As a reference by its generic name (12.3.2.1),
27 © As a defined assignment (subroutines only),
28 (d) In an expression as a defined operator (functions only), or
29 (e) In a context that requires it to be pure,

30 (2) The procedure has a dummy argument that
31 (a) has the ALLOCATABLE, ASYNCHRONOUS, OPTIONAL, POINTER, TARGET,
32 VALUE, or VOLATILE attribute,
33 (b) is an assumed-shape array,
34 © is of a parameterized derived type, or
35 (d) is polymorphic,

1 (3) The procedure has a result that
2 (a) is an array,
3 (b) is a pointer or is allocatable, or
4 © has a nonassumed type parameter value that is not an initialization expression,
5 (4) The procedure is elemental, or
6 (5) The procedure has the BIND attribute.

If you have Fortran 95/2003 Explained, see section 6.3.

Hope this clarifies things,
Mat