Fortran90 SIGSEGV: Segmentation fault

In summary, the conversation is about a project for a numerics class where the person has written a program in FORTRAN 90 to calculate the determinant of a matrix using Laplace. They have added a function to find the row/column with the most zeroes and reorganize the matrix, but when testing with larger matrices, they get a "SIGSEGV" error. The person has tried various solutions but has not been successful and is seeking help. They include the function in their post for reference.
  • #1
Ha1234
1
0
Hi,
I have a project for a class in numerics, where I have to write a program with FORTRAN 90 that calculates the determinant of a matrix using Laplace.
The program itself works now, but I have added a function that finds the row/column with most zeroes and reorganizes the matrix, so that they are in the first column. When I try it with small matrices, it works just fine. However (starting at 4x4) I get the following error:

SIGSEGV:Segmentation fault-invalid memory reference.
Backtrace for this error:
#0 6f6117be
#1 6f695deb
#2 004010f8

I have read pretty much every discussion about this I could find, but either the solutions did not apply to my code or the explanation was too complicated for me to unterstand...This is the first program ever that I wrote, so I am not an expert.

This is the function (whithout this function it works fine, so I gues the problem is somewhere in the function):
Fortran:
      function sortieren_Matrix(A,n) result(A_getauscht)

      implicit none

       !Festlegen der Variablen
         real                     A(n,n)                    !Matrix A
         real                     A_getauscht(n,n)        !Matrix mit vertauschten Zeilen und Spalten
         real                    merk                     !Hilfsvariablen
         integer                    n                         !Größe der Matrix
         integer                    zaehl                    !Zählt die Nullen
         integer                    merk1,merk2                !Hilfsvariablen
         integer                    zaehl_max1,zaehl_max2    !Anzahl Nullen (1-Zeile,2-Spalte)
         integer                    minus                    !negatives Vorzeichen
         integer                 i,j                        !Laufindices
   
    !Durchsuchen nach Zeile mit den meisten Nullen
   
         zaehl_max1=0.0
   
         do i=1,n
              zaehl=0.0
               do j=1,n
                   if(A(i,j) .eq. 0.0) then
                       zaehl=zaehl+1
                  end if
               end do
              if(zaehl .gt. zaehl_max1) then
               zaehl_max1=zaehl
               merk1=i                                     !Merken, in welcher Zeile die meisten Nullen sind
              end if
        end do
   
    !Durchsuchen nach Spalte mit den meisten Nullen
   
        zaehl_max2=0.0
   
         do i=1,n
            zaehl=0.0
                do j=1,n
                    if(A(j,i) .eq. 0.0) then
                        zaehl=zaehl+1
                    end if
                end do
            if(zaehl .gt. zaehl_max2) then
                zaehl_max2=zaehl
                merk2=i                                     !Merken, in welcher Spalte die meisten Nullen sind
            end if       
         end do
   
    !neu Ordnen der Matrix, sodass die erste Spalte, die mit den meisten Nullen ist
   
         if(zaehl_max1 .gt. zaehl_max2) then        !sehen ob in Spalte oder Zeile die meisten Nullen sind
            do i=1,n                                !Zeilen und Spalten vertauschen/transponieren
                do j=1,n
                    A_getauscht(i,j)=A(j,i)
                end do
            end do
            merk2=merk1
        else
            A_getauscht=A 
         end if
   
    !Vorzeichen der Determinante positiv oder negativ?
   
    minus=1
   
         if(merk2 .ne. 1) then            !Wenn bei einer Matrix Spalten oder Zeilen getauscht werden, ändert sich ihr Vrozeichen
          minus=-1
         end if
   
    !Ordnen, sodass Nullen in erster Spalte
   
         do i=1,n
            merk=A_getauscht(i,1)
             A_getauscht(i,1)=A_getauscht(i,merk2)
            A_getauscht(i,merk2)=merk
         end do
   
      end function sortieren_Matrix

I would really apprecaite it if someone had ideas what else I could be doing wrong!
Thank you!
 
Physics news on Phys.org

1. What is a "Fortran90 SIGSEGV: Segmentation fault" error?

A "Fortran90 SIGSEGV: Segmentation fault" error is an error that occurs when the program attempts to access a memory location that is not allocated or accessible. This can happen due to a variety of reasons, such as a null pointer, an out-of-bounds array index, or an invalid memory access.

2. How do I fix a "Fortran90 SIGSEGV: Segmentation fault" error?

The best way to fix a "Fortran90 SIGSEGV: Segmentation fault" error is to identify the source of the error. This can be done by using a debugger or by carefully reviewing the code. Once the source of the error is identified, it can be addressed by properly allocating memory, fixing array indices, or correcting any invalid memory accesses.

3. Can a "Fortran90 SIGSEGV: Segmentation fault" error be prevented?

Yes, a "Fortran90 SIGSEGV: Segmentation fault" error can be prevented by writing code that carefully manages memory usage and avoids any invalid memory accesses. It is also important to regularly test and debug the code to catch any potential errors.

4. Why does a "Fortran90 SIGSEGV: Segmentation fault" error occur?

A "Fortran90 SIGSEGV: Segmentation fault" error can occur due to a variety of reasons, such as a programming error, a memory allocation issue, or an invalid memory access. It is important to carefully review the code and use debugging tools to determine the specific cause of the error.

5. Can a "Fortran90 SIGSEGV: Segmentation fault" error be ignored?

No, a "Fortran90 SIGSEGV: Segmentation fault" error should not be ignored. Ignoring the error can lead to unexpected behavior and potentially cause the program to crash. It is important to address and fix the error to ensure the proper functioning of the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
4
Views
566
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Programming and Computer Science
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top