Fortran What Causes a Segmentation Fault in Fortran Programs?

  • Thread starter Thread starter pandroza
  • Start date Start date
  • Tags Tags
    Fault Fortran
Click For 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K