gpt4 book ai didi

exception - "should"时未引发 Ada CONSTRAINT_ERROR

转载 作者:行者123 更新时间:2023-12-04 20:49:03 24 4
gpt4 key购买 nike

我一直在看这个Ada 95 tutorial .我读到可以定义一个范围不同于标准范围的类型,如果程序试图超出这个范围,它会抛出一个错误。在我自己的程序中工作时,我注意到如果定义中的范围的末尾落在其基础类型的边界上,那么在分配超出该范围的值时,程序将不会引发 CONSTRAINT_ERROR。相反,它会很高兴地继续前进,然后环绕。我写了一个程序来明确显示这一点。

有谁知道解释这种行为的 Ada 规则?

-柯克

这是我终端的输出,源代码在其下方。

me@acheron:~/Dropbox/programs/ada$ gnatmake constraints.adb -f
gcc-4.6 -c constraints.adb
gnatbind -x constraints.ali
gnatlink constraints.ali
me@acheron:~/Dropbox/programs/ada$ ./constraints

Type ON has size: 7
It has a min/max of: 0 127
It's base has a min/max of: -128 127

Type UNDER has size: 7
It has a min/max of: 0 126
It's base has a min/max of: -128 127
The value of No_Error is: 245

raised CONSTRAINT_ERROR : constraints.adb:58 range check failed
me@acheron:~/Dropbox/programs/ada$

源代码:
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;

Procedure Constraints is

type UNDER is range 0..126;
type ON is range 0..127;
type OVER is range 0..128;

Error : UNDER := 0;
No_Error : ON := 0;

Index : INTEGER := 0;

begin
New_Line;
Put("Type ON has size: ");
Put(INTEGER(ON'SIZE));
New_Line;
Put("It has a min/max of: ");
Put(INTEGER(ON'FIRST));
Put(INTEGER(ON'LAST));
New_Line;
Put("It's base has a min/max of: ");
Put(INTEGER(ON'BASE'FIRST));
Put(INTEGER(ON'BASE'LAST));

New_Line;
New_Line;

Put("Type UNDER has size: ");
Put(INTEGER(UNDER'SIZE));
New_Line;
Put("It has a min/max of: ");
Put(INTEGER(UNDER'FIRST));
Put(INTEGER(UNDER'LAST));
New_Line;
Put("It's base has a min/max of: ");
Put(INTEGER(UNDER'BASE'FIRST));
Put(INTEGER(UNDER'BASE'LAST));

Safe_Loop:
loop
No_Error := No_Error + 1;
Index := Index + 1;
--Put(INTEGER(No_Error));
exit Safe_Loop when Index = 245;
end loop Safe_Loop;

New_Line;
Put("The value of No_Error is: ");
Put(INTEGER(No_Error));

Index := 0;

Crash_Loop:
loop
Error := Error + 1;
Index := Index + 1;
exit Crash_Loop when Index = 245;
end loop Crash_Loop;


end Constraints;

最佳答案

根据 the documentation :

Note again that -gnato is off by default, so overflow checking is not performed in default mode. This means that out of the box, with the default settings, GNAT does not do all the checks expected from the language description in the Ada Reference Manual. If you want all constraint checks to be performed, as described in this Manual, then you must explicitly use the -gnato switch either on the gnatmake or gcc command.



也就是说,该文件还声称:

Basically the rule is that in the default mode (-gnato not used), the generated code assures that all integer variables stay within their declared ranges, or within the base range if there is no declared range. This prevents any serious problems like indexes out of range for array operations. ¶ What is not checked in default mode is an overflow that results in an in-range, but incorrect value.



在您描述的情况下,这似乎是错误的,因为 No_Error确实最终完全超出了它的范围。所以这似乎超出了“不 [...] 从语言描述中得到的预期”,进入了“编译器错误”的范畴;但至少你应该能够通过添加 -gnato 来修复它旗帜。

关于exception - "should"时未引发 Ada CONSTRAINT_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12811093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com