2013-06-03 35 views
1

我在Pascal中實現鄰接列表(通過先讀取邊緣端點,然後使用動態數組爲每個節點的邊界列表分配所需的內存量)。該程序執行正常,給出正確的輸出,但在退出之前給出運行時錯誤216。
的代碼是:fpc Pascal運行時錯誤216在執行結束前

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. 

當在文件上運行

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

給輸出:

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 

一旦程序說 「再見」,什麼是程序執行是給運行時錯誤216?

回答

1

RTE 216通常是致命的例外。 GPF/SIGSEGV以及某些情況下的SIGILL/SIGBUS,這可能意味着您的程序在某處損壞了內存。

編譯與可能會幫助你發現錯誤(免費帕斯卡:-Criot)運行時檢查

+0

編譯時-Criot -gl,並給出錯誤 '運行時錯誤201'在'線22',這是' elist [edgebuf [i] [1]] [ptrs [edgebuf [i] [1]]]:= edgebuf [i] [2];' 找到它! http://www.freepascal.org/docs-html/rtl/system/setlength.html表示SetLength(Len)分配長度爲Len的數組,並且元素從索引0開始編號,因此計數從0運行到Len -1。我正在訪問1 Len。所以'setlength(elist [i],1 + ptrs [i]);'工作了! – Five