Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Segmentation fault

  1. Aug 16, 2011 #1
    Hi, I am new in fortran.
    I have this code in fortran 90, I get segmentation fault when I run my program, can you help me please, I am desperate!!!

    SUBROUTINE COUNTX(NX,ncov)
    IMPLICIT NONE
    INTEGER, INTENT(IN)::ncov
    INTEGER,DIMENSION(ncov),INTENT(INOUT)::NX(:)
    INTEGER:: AUX,i

    DO i=1,ncov
    print *,NX(i)
    END DO






    END SUBROUTINE



    PROGRAM minSC
    IMPLICIT NONE
    INTEGER::ncov,i
    INTEGER,ALLOCATABLE::NX(:)

    PRINT "(A)","Give me the value ncov "
    read(*,*),ncov
    allocate(NX(ncov))

    DO i=1,Ncov
    NX(i)=0

    END DO
    CALL COUNTX(NX,ncov)


    END PROGRAM
     
  2. jcsd
  3. Aug 16, 2011 #2

    Mark44

    Staff: Mentor

    I don't believe you should have that second comma in your read statement. IOW, I believe you should have this:
    Code (Text):

         read(*,*) ncov
     
    There might be other things causing your segmentation fault, but my knowledge of Fortran is from F77.
     
  4. Sep 4, 2011 #3
    I think you have compiled the code. If the problem occurs when you run executable file then it is classical memory problem. So may need to increase memory limits before running the exe-file. there are several type of commands ex:

    limit stacksize unlimited
    limit memoryuse unlimited
    limit vmemoryuse unlimited
    ulimit -s 8000000

    may be more.

    Just try!!!!
     
  5. Sep 17, 2011 #4
    Where does the segmentation fault occur exactly? Does your program print anything first, or does it happen right at the beginning?

    I actually got this same error yesterday and stumbled upon this thread while trying to solve it. In my case, it turns out that I had a simple typo when I was declaring external functions. So when I ran my program, it attempted to access a function that had not been declared, which means that it was trying to call a function with a memory address of 0, which belongs to the kernel. The program isn't allowed to access this memory location, hence the error.
     
  6. Sep 17, 2011 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    I can't see what it wrong here, but the way to solve these problems is isolate where the error is happening.

    If you don't have a development environment with a debugger, insert STOP statements to find out where the problem is.

    First try

    Code (Text):

    ...
    PRINT "(A)","Give me the value ncov "
    STOP
    ...
     
    Obviously that won't do anything useful, but it should run without crashing!

    Then do
    Code (Text):

    ...
    read(*,*),ncov
    print *, ncov
    STOP
    ...
     
    Note, also print out ncov so you know the program has read what you expected it to read.

    Then do
    Code (Text):

    ...
    allocate(NX(ncov))
    STOP
    ...
     
    etc.

    This might seem tedious, but it doesn't take long to do. When you know exactly which statement is causing the crash, it is a lot easier to figure out what is wrong.
     
  7. Oct 5, 2011 #6
    just to add to previous replies seg faults are usually caused when adressing a location outside array bounds (many times you try to reach the 0 element)

    also if you compile your code using the debug flag -g it will tell you where the fault occures.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran Segmentation fault
  1. Segmentation Fault (Replies: 9)

Loading...