在 Pascal 中实现冒泡排序时出现奇怪的运行时错误

发布于 2024-11-08 01:13:55 字数 1005 浏览 5 评论 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 只是定义为记录数组的类型,其中包含分数(整数)以及一堆其他变量。 trplayers 是上述记录的类型。我完全不知所措; FPC(不在调试模式下)会发出超出范围的错误,但在我的观察下,我看到我尝试读取的变量存在。非常感谢任何帮助!

This snippet not only causes a runtime error, it makes FPC close if I run it using the debugger.

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;

The code itself is part of a really big source file, I can post the whole thing but the responsible for the error is just that bunch of lines. The debugger terminates at line:

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

tplayers is just a type defined as an array of records that contain score (an integer) among a bunch of other variables. trplayers is the type of the aforementioned records. I'm at a total loss; FPC (while not under debugging mode) spits an out-of-range error but under my watches I see that the variables I'm trying to read exist. Any help is really appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-11-15 01:13:55

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

rAux:trplayers; have you typed a wrong symbol or the type here really contains "r" in its name?

蔚蓝源自深海 2024-11-15 01:13:55

它看起来有效(除了拼写错误)...所以让我们尝试一些简单的事情。
中止时“j”的值是多少?
如果调试器没有告诉您,请尝试

writeln ('j = ', j);

在“交换”调用之前添加:。

正如 Yochai 的问题所暗示的那样,您的数组的尺寸至少需要从
1(或更低)到 MAXPLAYERS(或更大)。 (即:0..MAXPLAYERS-1 不起作用,
但 1..MAXPLAYERS 应该。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文