Ada 中未初始化变量差异的技术解释?

Technical explanation for discrepancy in uninitialized variable in Ada?

据我了解,Ada 中未初始化的变量是未定义的,因此它可以是任何东西。通常,编译器会阻止您使用未初始化的变量,因为这很愚蠢。在尝试移动数组时,我注意到 GNAT 会让我访问数组中现在未初始化的成员。对于左移,新的最右边的成员将为 0,而对于右移,新的最左边的成员将为 6512352。无论数组的大小或移动了多少,这都是正确的。

这些数字有技术解释吗?

所有测试均在 Intel x64 上完成。我的 AMD x64 机器坏了,我的 RaspPi 几天前坏了。

轮班实施:

function Shift(Source : in Static_Array;
               Toward : in Direction;
               By : in Positive := 1)
               return Static_Array is
    Result : Static_Array(Source'Range);
begin
    case Toward is
        when Left =>
            Result(Result'First .. Result'Last-By) :=
                Source(Source'First+By .. Source'Last);
        when Right =>
            Result(Result'First+By .. Result'Last) :=
                Source(Source'First .. Source'Last-By);
    end case;
    return Result;
end Shift;

在测试文件中:

A : Static_Array := (1, 2, 3, 4, 5, 6, 7, 8);

结果:

结果是从测试程序生成的。首先是 运行,→ 之后是结果,= 之后是 "expected"。由于该值未初始化且未定义,因此我只是将 0 设置为有值。

未初始化的数组组件包含执行前内存中的任何残留物。虚假值可能看起来很刻板,部分原因是生成的代码分配堆栈帧的方式。扫一眼生成的assembly source may be helpful. Changing the execution environment will usually change the outcome. I see distinct differences with various array sizes. Try varying the optimization level using -On. Use a platform-specific memory altering command, e.g. purge,看看效果。