When I run my Fortran program I get a segmentation fault. What's wrong with my program

A segmentation fault means that the program referenced a memory address outside of the memory allotted to it. The most common cause of this error is an array index being outside the declared range.

This could occur from a miscalculation of an array index or the dimension of an array is not defined large enough. Example:

      % cat segmentation.f

          dimension x(5),y(5)
          print *, 'Enter size of arrays'
          read *,n
          do 10 i = 1,n
          x(i) = i**2
          y(i) = x(i)/10.0
       10 continue
          print *, x(n),y(n)
          end

      % f77 segmentation.f
        segmentation.f:
        MAIN:

      % a.out
        Enter size of arrays
        40
        Segmentation Fault (core dumped)
You can check the array subscripts by using the -C compiler option. For example,

      % f77 -C segmentation.f
        segmentation.f:
        MAIN:

      % a.out
        Enter size of arrays
        40
        Subscript out of range on file segmentation.f, line 5, procedure 
	MAIN.
        Attempt to access the 6-th element of variable x.
        Abort (core dumped)


Another cause of an array index being outside the declared range is when the variable that dynamically dimensions a subprogram array is TYPEd incorrectly.

Example:

     % cat segmentation2.f
         dimension x(5),y(5)
         size = 5
         call sub(x,y,size)
         end
         subroutine sub(x,y,n)
         dimension x(n),y(n)
         do 10 i = 1,n
           x(i) = i**2
           y(i) = x(i)/10.0
   10    continue
         print *, x(n),y(n)
         end

     % f77 segmentation2.f
       segmentation2.f:
       MAIN:
           sub:
     % a.out

     *** TERMINATING a.out
     *** Received signal 11 (SIGSEGV)
 Segmentation Fault (core dumped)


University of Delaware
October 23, 1994