私はサブルーチンとそれを呼び出すいくつかのFortranコードを持っています。定義されたウィンドウサイズを使用して行列の要素の平均を計算することになっています。たとえば、winsize = 2を使用して(10,10)配列のサブルーチンを呼び出すと、(5,5)配列が返されます。示すようにFortranの "end program"文のセグメンテーションフォルト
コードは次のとおりです。
SUBROUTINE avgwin(ts, sizelat,sizelon,winsize,size2,size3,ts_new)
implicit none
double precision, dimension(10,sizelat,sizelon) :: ts
double precision, dimension(winsize,winsize) :: store
double precision, dimension(10,size2,size3) :: ts_new
double precision :: par,ave
integer :: sizelat, sizelon,i,j,k,winsize,size2,size3
integer :: A, B,p,m,numb
A=0
B=0
par = 11 !Hypothetical value to be excluded
do i=1,10 !Looping through time
do j=1,sizelat !Looping through latitude
if ((j+winsize) > sizelat) then !Checks if it will exceed bounds
exit !If it'll exceed, discard remaining cells
end if
do k=1,sizelon !Looping through longitude
if ((k+winsize)>sizelon) then
exit
end if
store = ts(i,j:j+winsize,k:k+winsize) !Gets the values for that window
where (store == par) store = -99 !Replaces masked with -99
ave = 0
numb = 0 !Variable to count
do p=1,winsize
do m=1,winsize
if (store(p,m)==-99) then !Evaluates if it's masked, i.e., =-99
ave = ave
else
ave = ave + store(p,m) !Sum of existent values
numb = numb +1 !Updates counting variable
end if
end do
end do
ave = ave/numb !Calculates the mean
ts_new(i,A,B) = ave
B=B+1
end do
B=0
A=A+1
end do
A=0
B=0
end do
END SUBROUTINE
program testefor
implicit none
double precision, dimension(10,10,10) :: teste
double precision, dimension(10,5,5) :: oi
integer :: i,j,k
do i=1,10
do j=1,10
do k=1,10
teste(i,j,k)=i
end do
end do
end do
CALL avgwin(teste,10,10,2,5,5,oi)
print*, oi(1,5,5)
end program testefor
私はそれを実行している、しかし、私は、セグメンテーションフォールトを取得します。私はGDBでデバッグしようとしましたが、驚いたことに、正しい結果を返しますが、プログラムを終了するときにはsegfaultsを返します。
Breakpoint 1, testefor() at testefor.f90:56
56 do i=1,10
(gdb) cont
Continuing.
1.0000000000000000
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400f73 in testefor() at testefor.f90:67
67 end program testefor
したがって、プログラムは正しい(1,5,5)要素= 1.0を返していますが、別の場所でフォールトしています。
誰かが問題を特定するのを手助けできますか?おかげで