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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

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