问题是:
Is a wrap expected within the Byte() cast?
让我们将反汇编与溢出检查打开/关闭进行比较.
{$Q+}
Project71.dpr.21: for ptr:= 0 to 10 do Values[Byte(ptr-5)]:= 1;
0041D568 33DB xor ebx,ebx
0041D56A 0FB6C3 movzx eax,bl
0041D56D 83E805 sub eax,$05
0041D570 7105 jno $0041d577
0041D572 E82D8DFEFF call @IntOver
0041D577 0FB6C0 movzx eax,al
0041D57A C704870000803F mov [edi+eax*4],$3f800000
0041D581 43 inc ebx
0041D582 80FB0B cmp bl,$0b
0041D585 75E3 jnz $0041d56a
{$Q-}
Project71.dpr.21: for ptr:= 0 to 10 do Values[Byte(ptr-5)]:= 1;
0041D566 B30B mov bl,$0b
0041D568 B808584200 mov eax,$00425808
0041D56D C7000000803F mov [eax],$3f800000
0041D573 83C004 add eax,$04
0041D576 FECB dec bl
0041D578 75F3 jnz $0041d56d
使用{$Q}包装有效,而使用{$Q-}包装不起作用,并且当设置{$R}时,编译器不会为错误的数组索引生成范围错误.
因此,对我来说结论是:由于范围检查不会为数组索引超出边界生成运行时错误,因此需要进行换行.
这一事实进一步证明了在启用溢出检查时完成换行的事实.
这应该报告为编译器中的错误.
注意:@Rudy在他的回答中提供了一种解决方法.
附录:
以下代码:
for ptr:= 0 to 10 do WriteLn(Byte(ptr-5));
产生:
251
252
253
254
255
0
1
2
3
4
5
对于范围/溢出检查的所有组合.
同样值[Byte(-1)]:= 1;为所有编译器选项的值[255]分配1.
The resulting value is obtained by converting the expression in parentheses. This may involve truncation or extension if the size of the specified type differs from that of the expression. The expression’s sign is always preserved.