Search code examples
exceptioncobolgnucobol

Segmentation fault error in my COBOL application


I wrote this COBOL application to perform a prime factorization, it is not yet completed.

When I run the code, I get this error:

unknown (signal)

cobc: aborting compile of jdoodle.cobc at line 68 (PROGRAM-ID: primzahl_check)
cobc: Please report this!
bash: line 1:    14 Segmentation fault      (core dumped) cobc -x -free -O jdoodle.cobc -o jdoodle

I use JDoodle as my IDE and it uses GNUCobol 3.2.0.

Searching in the documentation, trying to remove code that could cause this conflict.

My code:

identification division.
program-id. primfaktorzerlegung.
data division.
working-storage section.
01 divident pic 9(5).
01 rest pic 9v9.
01 divisor pic 9(5).
01 ergebnis pic 9(5).

*> Kurze Erklärung:
*> Primfaktorzerlegung der Zahl 20.
*> 20 / 2 = 10
*> 10 / 2 = 5
*> 5 / 2 = 2.5
*> Also lautet die Primfaktorzerlegung: von 20 = 2 *> 2 *> 5
*> Bis zu der Zahl wo die Division einen Rest ergibt.

*> Eine Liste für alle Zahlen von 2 bis 100 mit den dazugehörigen Zerlegungen ausgeben

procedure division.
display 'Programm zur Darstellung der Primfaktorzerlegung mit einer gegebenen Zahl'.
display ' '.

    call 'primzahl_check' using divident rest divisor ergebnis.

end program primfaktorzerlegung.

identification division.
program-id. primzahl_check.
data division.
linkage section.
01 divident pic 9(5).
01 rest pic 9v9.
01 divisor pic 9(5).
01 ergebnis pic 9(5).
01 zerlegt pic x(5).
01 priv_divident pic 9(5).
01 zahlenarray.
05 zahlen occurs 4 times indexed by z_idx.
10 z_zahl pic s9(5).
01 i pic 9(5).
01 quotient pic 9(5).
*> Überprüfen ob es bei der Zahl um eine Primzahl handelt
procedure division using divident rest divisor ergebnis.
perform until divident = 3

        move 2 to divisor
        move 1 to rest 
    
        perform until rest = 0 or divisor >= divident
            
            divide divident by divisor giving ergebnis remainder rest
            
            add 1 to divisor
        end-perform
        
        if divident = divisor then
            display ' '
            display divisor " ist eine Primzahl"
            display ' '
        else
            call 'primfaktorzerlegung' using divisor zerlegt divident priv_divident zahlenarray i quotient
        
        
        add 1 to divident
    end-perform.

end program primzahl_check.

identification division.
program-id. primfaktorzerlegung.
data division.
01 divisor pic 9(5).
01 zerlegt pic x(5).
01 divident pic 9(5).
01 priv_divident pic 9(5).
01 zahlenarray.
05 zahlen occurs 4 times indexed by z_idx.
10 z_zahl pic s9(5).
01 i pic 9(5).
01 quotient pic 9(5).
01 rest pic 9v9.

procedure division using divisor zerlegt divident priv_divident z_zahl i quotient

    *> Primfaktorzerlegung
    move 2 to divisor.
    move 'false' to zerlegt.
    
    *> Ursprünglicher Divident speichern, in diesem Fall die 20
    move divident to priv_divident
    move 1 to z_idx
    move 2 to i
    move priv_divident to z_zahl(z_idx)
    move 2 to divident.
    
    perform until zerlegt = 'true'
        
        divide divident by divisor giving quotient remainder rest
        
        move quotient to divident
            
        if rest = 0 then
               
            move i to z_idx
            
            move divisor to z_zahl(z_idx)
                
            add 1 to i
            
        else
            move i to z_idx
            
            move quotient to z_zahl(z_idx)
                
            move 'true' to zerlegt
            
        call 'zahlen_ausgeben' using 
            
    end-perform.

end program primfaktorzerlegung.

identification division.
program-id. zahlen_ausgeben.
data division.
01 y pic 9(5).
01 zahlenarray.
05 zahlen occurs 4 times indexed by z_idx.
10 z_zahl pic s9(5).
01 zahl_prnt pic z.

procedure division using y z_zahl zahl_prnt

perform varying y from 1 by 1 until y = 5

        move y to z_idx
        
        move z_zahl(z_idx) to zahl_prnt
        
        display zahl_prnt
        
    end-perform.

end program zahlen_ausgeben.

`


Solution

  • You are missing a couple of end-if statements!

    Plus you need to define variables passed to your subroutines in a LINKAGE SECTION.

    "primfaktorzerlegung" is defiend twice!