Recent

Author Topic: A Problem (?) With For -- To -- Do  (Read 2633 times)

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 750
A Problem (?) With For -- To -- Do
« on: April 10, 2013, 07:39:28 pm »
This is a dumb mistake, but I wonder if FPC should issue an error when it occurs?  (Or perhaps it DOES issue one if you have the compiler switches set correctly, and I don't?)

procedure foo(n: integer);
var i: byte;
begin
  for i := 0 to N do <something>;
end;

If the argument N >255, the for--to--do statement seems to simply not execute at all, with no warning to let you know that the variable i really needs to be an integer, not a byte. 

Does range checking turned on catch this?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: A Problem (?) With For -- To -- Do
« Reply #1 on: April 10, 2013, 07:51:00 pm »
Quote
If the argument N >255, the for--to--do statement seems to simply not execute at all, with no warning to let you know that the variable i really needs to be an integer, not a byte.
Not true, it behaves like (N and 255).
Code: [Select]
begin
  n:=257;
  for i := 0 to N do <something>;
end;
will be like
Code: [Select]
for i:=0 to 1 doI think this is by design, it is not a bug.
Quote
Does range checking turned on catch this?
It raises exception here.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 750
Re: A Problem (?) With For -- To -- Do
« Reply #2 on: April 11, 2013, 12:38:23 am »
Ah -- interesting.  Thanks.

 

TinyPortal © 2005-2018