Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-10

Range checking in a for loop

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

leledumbo

Consider the following program:

program test;
var
  a: array [1..3] of Integer;
  i: Integer;
begin
  for i := 1 to 4 do
    a[i] := i;
end.

The compiler will accept this code happily, despite the fact that there's an out of bounds array index when i = 4.

Since the behavior of for loop in FPC (at least in non Delphi / TP mode) is deterministic, it should be possible to check whether the index is out of bounds or not. Isn't it? Or is there any situation where it's not?

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Range-checking-in-a-for-loop-tp3235085p3235085.html Sent from the Free Pascal - General mailing list .
David W Noon
to leledumbo

On Mon, 25 Oct 2010 01:06:16 -0700 (PDT), leledumbo wrote about [fpc-pascal] Range checking in a for loop:

[snip]

Show quoted text 12 lines
program test; var a: array [1..3] of Integer; i: Integer; begin for i := 1 to 4 do a[i] := i; end. The compiler will accept this code happily, despite the fact that there's an out of bounds array index when i = 4.

There is no reason in the for-loop construct that "i" cannot have the value 4; it is only a problem when "i" is used as a subscript on the array "a".

Try declaring

   i : 1..3;

instead, as that range matches the array's bounds. You should then get a range check at compile time. It is also the idiomatic Pascal way of doing this.

Regards, Dave [RLU #314465] ======================================================================= (David W Noon) =======================================================================
Paul Ishenin
to leledumbo: Consider the following program: program test; var a: array [1..3] of Integer; i: Integer;…
leledumbo wrote 18 lines
Consider the following program: program test; var a: array [1..3] of Integer; i: Integer; begin for i := 1 to 4 do a[i] := i; end. The compiler will accept this code happily, despite the fact that there's an out of bounds array index when i = 4. Since the behavior of for loop in FPC (at least in non Delphi / TP mode) is deterministic, it should be possible to check whether the index is out of bounds or not. Isn't it? Or is there any situation where it's not?

a[i] is an expression. You can write a[(i+j)/n] there and compiler will fail to calculate whether the expression match the bound in this case.

Moreover, to create a dynamic array you usually use ^a[0..0] type and it would be sad if compiler rejected any index 0 for this case.

Anyway, if you want to be sure you are in array bounds just use Low(a) as the lower bound and High(a) as the high bound.

Best regards,
Paul Ishenin.

Jonas Maebe
to Jonas Maebe
Jonas Maebe wrote 10 lines
On 25 Oct 2010, at 13:24, David W Noon wrote: > Try declaring > i : 1..3; > instead, as that range matches the array's bounds. You should then > get > a range check at compile time. With current released versions, you won't get such a compile time error.

... if range checking is disabled (then you'll only get a warning). When range checking is enabled, you do get an error already.

I fixed that just yesterday in svn though (r16213).

and now you get an error under all circumstances (common sense and Delphi-compatible).

Jonas

leledumbo
to Jonas Maebe

OK, thanks. I just want to know why such a thing cannot be detected at compile time. So, a good typing practice is the solution, right? Like this:

program test;
type
  TArrayRange = 1..3;
  TArray = array[TArrayRange] of Integer;
var
  a: TArray;
  i: TArrayRange;
begin
  for i := 1 to 4 do
    a[i] := i;
end.
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Range-checking-in-a-for-loop-tp3235085p3235405.html Sent from the Free Pascal - General mailing list .