On Dec 26, 2011, at 4:06 PM, Knut wrote:
> Hi,
>
> do you have any idea why
>
> call cnv_3d(imin,jmin,imax,jmax,kmin,kmax,az,rho-1000.,rho_missing, &
> imin,imax,jmin,jmax,0,kmax,ws)
>
> can cause a segmentation fault, whereas
>
> call cnv_3d(imin,jmin,imax,jmax,kmin,kmax,az,rho,rho_missing, &
> imin,imax,jmin,jmax,0,kmax,ws)
>
> does not?
>
> Thanks,
> Knut
indeed interesting, but not really surprising,
you must consider that fortran does not transfer the arrays, but only the pointer to the array.
As example program below, there are 3 different calls of a subroutine for which you naively could expect
to return all the same output values, but indeed they do NOT!:
Putting brackets around the field or subtracting a value in the function call (instead of before)
give quite (surprising?) different results (for ifort and gfortran)
Proposal:
Never ever use something like (rho-1000.) in subroutine calls if you want to have reproducible results!
Happy programming, Adolf
program kurt
integer :: i
real, dimension(100) :: xx,yy,zz
i = 100
xx = 0
zz=xx
xx =xx - 100
yy = xx
call cnv_3d(xx,i)
write(*,*) 'Call 1:',xx
call cnv_3d((yy),i)
write(*,*) 'Call 2:',yy
call cnv_3d(zz-100,i)
write(*,*) 'Call 3:',zz
end
subroutine cnv_3d(yy,n)
integer, intent(in) :: n
real, intent(inout), dimension(n) :: yy
integer :: i
do i = 1,n
yy(i) = yy(i)+i
end do
end subroutine cnv_3d