Fortran90 SIGSEGV: Segmentation fault

Click For Summary
SUMMARY

The forum discussion addresses a segmentation fault (SIGSEGV) encountered in a Fortran 90 program designed to calculate the determinant of a matrix using Laplace's method. The issue arises specifically when the user attempts to reorganize the matrix based on the number of zeroes in its rows and columns, particularly for matrices of size 4x4 and larger. The function sortieren_Matrix is identified as the source of the error, likely due to improper handling of matrix dimensions or memory allocation. Users are encouraged to review their indexing and ensure that all variables are correctly initialized and used.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Knowledge of matrix operations and determinants
  • Familiarity with memory management and segmentation faults
  • Experience with debugging techniques in Fortran
NEXT STEPS
  • Review Fortran 90 array indexing and memory allocation practices
  • Learn about debugging tools for Fortran, such as GDB or Valgrind
  • Explore matrix manipulation techniques in Fortran 90
  • Investigate best practices for handling zero elements in matrices
USEFUL FOR

This discussion is beneficial for students and developers working with Fortran 90, particularly those involved in numerical computing and matrix operations. It is also useful for anyone troubleshooting segmentation faults in their Fortran programs.

Ha1234
Messages
1
Reaction score
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

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 4 ·
Replies
4
Views
2K