我无法弄清楚为什么下面的代码失败了。我有自己的相同脚本版本,返回相同的错误。在任何一种情况下,我无法弄清楚为什么错误仍然存在。
这是错误:
raised CONSTRAINT_ERROR : main2.adb:32 index check failed
就是这一行:
temp(i) := a(loop_high);
有人知道可能导致这种情况的原因吗?
with Text_IO;
with Ada.Integer_Text_IO;
procedure main2 is
use Text_IO;
use Ada.Integer_Text_IO;
type int_array is array(1..5) of integer;
tosort:int_array;
procedure merge (a:in out int_array; low,mid,high:in integer) is
temp: int_array;
choose1: boolean;
loop_low,loop_high:integer;
begin
loop_low:=low;
loop_high:=high;
for i in low..high loop
if (loop_low>mid) then choose1:=false;
elsif (loop_high>high) then choose1:=true;
else choose1:= a(loop_low)<a(loop_high);
end if; -- choose which side
if choose1 then -- choose from low side
temp(i):=a(loop_low);
loop_low:=loop_low+1;
else
temp(i):=a(loop_high); -- choose from high side
loop_high:=loop_high+1;
end if;
end loop;
a:=temp;
end merge;
procedure mergesort(a: in out int_array;low,high:integer) is
mid:integer;
begin
if low<high then
mid:= (high+low)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
end if;
end mergesort;
begin
tosort := (171, 201, 397, 10, -381);
mergesort(tosort,1,5);
end main2;
答案 0 :(得分:3)
我建议使用-gnateE进行编译,这将为编译器生成的异常提供更多信息。在这种特定情况下,它应该告诉您哪个值超出了哪个范围