[fpc-pascal] Question: range checking for string indexing
Vinzent Höfler
JeLlyFish.software at gmx.net
Wed Oct 29 21:58:10 CET 2008
Tom Verhoeff wrote:
> I am surprised that for ShortString, the range is not
> also 1 <= i <= Length(s).
>
> IIRC Turbo Pascal only allowed you to access s[0] when range checking
> was off, and similarly for s[i] with i > Length(s).
No. See transcript:
-- 8< -- the program --
{$R+}
var
s1 : String;
s2 : String[40];
begin
s1 := '';
s2 := '';
WriteLn (Length (s1));
WriteLn (Length (s2));
WriteLn (Ord (s1[0]));
WriteLn (Ord (s2[0]));
WriteLn (Ord (s1[255]));
WriteLn (Ord (s2[255]));
end.
-- 8< --
results in:
-- 8< --
Borland Pascal Version 7.0 Copyright (c) 1983,92 Borland International
TEST.PAS(18): Error 76: Constant out of range.
WriteLn (Ord (s2[255]));
^
-- 8< --
Commenting out the offending last line of code:
-- 8< --
Borland Pascal Version 7.0 Copyright (c) 1983,92 Borland International
TEST.PAS(19)
19 lines, 2432 bytes code, 968 bytes data.
-- 8< --
Running the program:
-- 8< --
.\TEST.EXE
0
0
0
0
0
-- 8< --
No errors, although the index exceeds the actual length of the string.
Vinzent.
More information about the fpc-pascal
mailing list