runtime-errordynamic-arraysfreepascaladjacency-matrixgeneral-protection-fault

What is the cause of run‐time error 216 before execution ends?


I implemented an adjacency list in Free Pascal (by first reading edge end points, and then using dynamic arrays to assign required amount of memory to elist of each node). The program executes fine, produces the correct output, yet crashes with a run‐time error 216 just before exiting. The code is:

type aptr = array of longint;
var edgebuf:array[1..200000,1..2] of longint;
    ptrs:array[1..100000] of longint;
    i,j,n,m:longint;
    elist:array[1..100000] of aptr;

{main}
begin
    readln(n,m);
    fillchar(ptrs,sizeof(ptrs),#0);
    for i:=1 to m do begin
    readln(edgebuf[i][1],edgebuf[i][2]);
    inc(ptrs[edgebuf[i][1]]);
    end;
    for i:=1 to n do begin
    setlength(elist[i],ptrs[i]);
    end;
    fillchar(ptrs,sizeof(ptrs),#0);

    for i:=1 to m do begin
    inc(ptrs[edgebuf[i][1]]);
    elist[edgebuf[i][1]][ptrs[edgebuf[i][1]]]:=edgebuf[i][2];
    end;

    for i:=1 to n do begin
    writeln(i,' begins');
    for j:=1 to ptrs[i] do begin
        write(j,' ',elist[i][j],' ');
    end;
    writeln();
    writeln(i,' ends');
    end;
    writeln('bye');
end.

Given the input

4 5
1 2
3 2
4 3
2 1
2 3

the program prints

1 begins
1 2 
1 ends
2 begins
1 1 2 3 
2 ends
3 begins
1 2 
3 ends
4 begins
1 3 
4 ends
bye
Runtime error 216 at $0000000000416644
  $0000000000416644
  $00000000004138FB
  $0000000000413740
  $0000000000400645
  $00000000004145D2
  $0000000000400180

Once the main program prints “bye”, what is the program executing that is causing the RTE 216?


Solution

  • RTE 216 is in general fatal exceptions. GPF/SIGSEGV and in some cases SIGILL/SIGBUS, and that probably means that your program corrupts memory somewhere.

    Compile with runtime checks on might help you find errors (Free Pascal : -Criot )