2

I have a fortran program. A subroutine is as below.The program gives segmentation fault after executing line 1434 and printing the below:

i:          115           256             2
Segmentation fault (core dumped)

The parameters are n1=258, n2=258, and n3=258. nr=46480. Why does 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 do default(shared) private(i1,i2,i3)
1431       do  i3=1,n3
1432          do  i2=1,n2
1433             do  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
7
  • 1
    It probably means that the array you're passing as z isn't as big as you said it was. Since we can't see the definition of the array in the calling code, we can't do more than guess, though. Commented Aug 20, 2013 at 3:42
  • The definition is double precision u(nr),v(nv),r(nr),a(0:3),c(0:3) common /noautom/ u,v,r . nr is 46480 Commented Aug 20, 2013 at 4:03
  • 1
    Please (a) edit the information into the question, and (b) show the function call. However, none of the arrays is anywhere near big enough — 258*258*258 = 17173512 — so the core dump is almost inevitable. Commented Aug 20, 2013 at 4:09
  • 1
    Maybe OT: It could be faster to simply write z=0.0D0. Commented Aug 20, 2013 at 8:12
  • 1
    @Stefan: not only faster (possibly), but also much harder to assign a value outside the bounds of the array. Commented Aug 20, 2013 at 8:35

1 Answer 1

2

Your variable definition sets aside storage for 46480 double in array u (and also sets aside as much space for v and r.

Your function call to zero3() claims there is enough storage for 258*258*258 = 17173512 doubles in the array you are passing.

When it tries to access an element far enough outside the bounds of the actual array, the program crashes — you are trying to access memory that is not allocated to your program.

Either you need to change nr to be a smaller number (35*35*35 = 42875, so zero3(u, 35, 35, 35) should be safe (non-crashing), or you need to allocate more space for u:

double u(258,258,258)

or something similar (it's a while since I last wrote Fortran; the standard was Fortran 77 at the time).

Sign up to request clarification or add additional context in comments.

2 Comments

I am also facing the same issue, but in my case even if I add a WRITE(*,*) 'Hello World' statement after all the INCLUDE statements & Variable Declarations are over, it gives me Segmentation Fault error message. Can you pls help me in understanding what eactly is causing this issue?
If your problem is 'exactly' the same, you have allocated 100 KiB space (say) and are trying to store multiple MiB of data into the 100 KiB, and the crash is the computer's way of saying it is not happy with you for trying to squeeze a couple of gallons into a pint pot. The fix is the same as for this question: either cut down the data you try to fit in the pot to less than a pint, or increase the size of the pot so that it is at least a couple of gallons big. Or, put another way, get the sizes right. If that's insufficient, ask a new question, making sure there's enough information in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.