linux - Segmentation fault happens when accessing array in a fortran program -
i have fortran program. subroutine below.the program gives segmentation fault after executing line 1434 , printing below:
i: 115 256 2 segmentation fault (core dumped)
the parameters n1=258, n2=258, , n3=258. nr=46480. why segmentation fault happen?
75 double precision u(nr),v(nv),r(nr),a(0:3),c(0:3) 76 common /noautom/ u,v,r ...... 196 call zero3(u,n1,n2,n3) ...... 1418 subroutine zero3(z,n1,n2,n3) 1419 1420 c--------------------------------------------------------------------- 1421 c--------------------------------------------------------------------- 1422 1423 implicit none 1424 1425 1426 integer n1, n2, n3 1427 double precision z(n1,n2,n3) 1428 integer i1, i2, i3 1429 1430 !$omp parallel default(shared) private(i1,i2,i3) 1431 i3=1,n3 1432 i2=1,n2 1433 i1=1,n1 1434 print*,"i: ",i1, " ", i2 , " " ,i3 1435 z(i1,i2,i3)=0.0d0 1436 enddo 1437 enddo 1438 enddo 1439 1440 return 1441 end
your variable definition sets aside storage 46480 double
in array u
(and sets aside space v
, r
.
your function call zero3()
claims there enough storage 258*258*258 = 17173512 doubles in array passing.
when tries access element far enough outside bounds of actual array, program crashes — trying access memory not allocated program.
either need change nr
smaller number (35*35*35 = 42875, zero3(u, 35, 35, 35)
should safe (non-crashing), or need allocate more space u
:
double u(258,258,258)
or similar (it's while since last wrote fortran; standard fortran 77 @ time).
Comments
Post a Comment