Recent

Author Topic: Nested procedure and for variable  (Read 5338 times)

BenLaz

  • New Member
  • *
  • Posts: 38
Nested procedure and for variable
« on: August 16, 2014, 06:32:42 pm »
Hello,

I have a small question :

In a for statement, the variable cannot be affected by a direct affectation or by passing it to a procedure as var parameter (but it can me modified via its adress wich the compiler cannot detect).

But in the case of a nested procedure, you can modify the variable if theis way :

procedure Test;
var
  iCount : Integer;
    procedure NestedProc;
    begin
      iCount := 10000;
    end; 
begin
  for iCount := 0 to 10 do
  begin
    NestedProc;
  end;
end;

Does anyone know if it's really "legal" to do this and if delphi autorize that construction (i don't have a Delphi at home so i'll check later at work).

Best regards

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11449
  • FPC developer.
Re: Nested procedure and for variable
« Reply #1 on: August 16, 2014, 06:39:44 pm »
It seems to work with both Delphi and FPC, but I don't know if that because this code is simple or not. (it is possible that the compilers decide to force the loopvar to a variable, essentially changing the for to a while, and then it would be safe).

However, working around errors like this is not smart. If it is forbidden, there is a good reason. Use break or rewrite the for loop using a different looping concept (while/repeat)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Nested procedure and for variable
« Reply #2 on: August 16, 2014, 06:40:57 pm »
'Legal' in the sense of 'compilable', yes. But completely pointless. A hack to avoid.
Why would you want a for loop declared to run for 11 iterations that only makes one iteration?
If you want to fiddle with loop counters, much better to use a while or repeat construct.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Nested procedure and for variable
« Reply #3 on: August 16, 2014, 07:04:12 pm »
Do you complain that the compiler did not make a complete work? Should it make it? Change the counter of a for loop is not legal for the language.

BenLaz

  • New Member
  • *
  • Posts: 38
Re: Nested procedure and for variable
« Reply #4 on: August 16, 2014, 07:19:22 pm »
Thank you for your answsers.

In fact my question is just theorical, i don't want to use this kind of construction; but i just want to know why the compiler doesn't complain about that.

Best regards

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Nested procedure and for variable
« Reply #5 on: August 16, 2014, 07:34:20 pm »
Well, in my opinion, just because it does not need to write your code for you.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Nested procedure and for variable
« Reply #6 on: August 16, 2014, 07:36:45 pm »
Funny fact.
In TP6 the above mentioned code causes an infinite loop, in fpc it doesn't.

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Nested procedure and for variable
« Reply #7 on: August 17, 2014, 01:01:41 am »
Cases like this are possible to detect IMO, but will require much more sophisticated checking algorithm which might slow down the compilation.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Nested procedure and for variable
« Reply #8 on: August 17, 2014, 01:27:41 am »
The compiler cannot detect stupidity.
Guess what happens here.....
Code: [Select]
var i: integer;
begin
  for i := 1 to 3 do
  begin
    Integer((@i)^) := 1;
  end;
end;
« Last Edit: August 17, 2014, 01:30:56 am by eny »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11449
  • FPC developer.
Re: Nested procedure and for variable
« Reply #9 on: August 17, 2014, 01:32:50 am »
The compiler /could/ detect this. But since Delphi behaves the same, I guess it is a detail that slipped in in the past, and then was kept as compatibility item.

 

TinyPortal © 2005-2018