0

这个片段不仅会导致运行时错误,而且如果我使用调试器运行它,它会使 FPC 关闭。

procedure sortplayersbyscore(var vAux:tplayers);

    procedure swap(var a:trplayers;var b:trplayers);

    var
        rAux:trplayers;

    begin
        rAux:=a;
        a:=b;
        b:=rAux;
    end;

var
    i,j:integer;
    sorted:boolean;

begin
    vAux:=playersarray;
    i:=1;
    sorted:=false;
    while (i <= MAXPLAYERS -1) and not sorted do
    begin
        j:=1;
        sorted:=true;
        while (j <= MAXPLAYERS -i) do
        begin
            if (vAux[j].score < vAux[j+1].score) then
            begin
                swap(vAux[j],vAux[j+1]);
                sorted:=false;
            end;
            inc(j);
        end;
        inc(i);
    end;
end;

代码本身是一个非常大的源文件的一部分,我可以发布整个内容,但导致错误的只是那一堆行。调试器在以下行终止:

swap(vAux[j],vAux[j+1]);

tplayers 只是一种定义为记录数组的类型,其中包含一堆其他变量中的分数(整数)。tplayers 是上述记录的类型。我完全不知所措;FPC(虽然不在调试模式下)吐出一个超出范围的错误,但在我的观察下,我看到我试图读取的变量存在。非常感谢任何帮助!

4

2 回答 2

1

It looks valid (other than typos) ... so let's try something simple.
What's the value of "j" when you abort? If the debugger won't tell you, try adding:

writeln ('j = ', j);

just before the "swap" call.

As Yochai's question implied, your array needs to be dimensioned at least from 1 (or lower) to MAXPLAYERS (or larger). (I.e.: 0..MAXPLAYERS-1 would not work, but 1..MAXPLAYERS should.)

于 2011-05-20T21:34:52.187 回答
1

rAux:trplayers;您是否输入了错误的符号或此处的类型名称中确实包含“r”?

于 2011-05-19T19:25:49.810 回答