Fortran What Causes a Segmentation Fault in Fortran Programs?

  • Thread starter Thread starter pandroza
  • Start date Start date
  • Tags Tags
    Fault Fortran
AI Thread Summary
The discussion centers around a Fortran 90 code that is causing a segmentation fault when executed. The user seeks assistance to resolve this issue. Key points include the suggestion to correct the read statement by removing an unnecessary comma, which may contribute to the error. Additionally, users highlight the importance of identifying where the segmentation fault occurs, recommending the use of STOP statements to isolate the problem. They mention that segmentation faults often arise from accessing memory outside array bounds, and one user shares their experience of encountering a similar error due to a typo in function declarations. Compiling the code with a debug flag is also advised to pinpoint the fault location more effectively. Overall, the discussion emphasizes debugging techniques and common pitfalls in Fortran programming that can lead to segmentation faults.
pandroza
Messages
1
Reaction score
0
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
 
Technology news on Phys.org
pandroza said:
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

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

There might be other things causing your segmentation fault, but my knowledge of Fortran is from F77.
 
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!
 
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.
 
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:
...
PRINT "(A)","Give me the value ncov "
STOP
...

Obviously that won't do anything useful, but it should run without crashing!

Then do
Code:
...
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:
...
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
12
Views
3K
Replies
4
Views
2K
Replies
5
Views
2K
Replies
5
Views
8K
Replies
4
Views
2K
Replies
4
Views
2K
Back
Top