Recent

Author Topic: For variable being read after loop... Any way to get an error or warning on it?  (Read 13114 times)

kupferstecher

  • Hero Member
  • *****
  • Posts: 590
It would restrict the compiler from some optimizations of the for-loop which it currently can do as long as the loop does not contain a Break or goto.
The end value in case the loop is completely ran through is already known when starting the loop (isn't?), so it should be no more than one assignment after the loop was finished and the loop in its inner could still be optimised in any way. For the break and goto thing the missing optimisation would result in slower speed in each loop cycle, so I'd assume the impact much higher. Linear impact instead of constant impact.
For the after-loop-assignment the compiler could even optimize it away if the variable is not used afterwards. So this could be done without any performance impact on "normal" for loops. But I understand that it is an implementation effort for a not overly useful feature.

ASBzone

  • Hero Member
  • *****
  • Posts: 713
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Currently it prints 10, which is wrong since there is no assignment number := 10. Only the for‑loop could have possibly made such an assignment, but it is not supposed to do that according to the standards.

While I agree that 11 would have been an more expected result than 10 (for me), I think we need to agree what it means when we read that under circumstances a particular variable or outcome will be undefined.

I read that as there being no defined or documented outcome for what value the variable will be holding when the loop has been exited.  For a variety of reasons, that space in memory could have been used in any way that the compiler writers desired upon exit, since they are under no obligation to have a pre-determined value there after completion of the loop.

So, 10 is just as "valid" an outcome as any at that point.


-ASB: https://www.BrainWaveCC.com/

Lazarus v3.5.0.0 (2216170cde) / FPC v3.2.3-1387-g3795cadbc8
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Zvoni

  • Hero Member
  • *****
  • Posts: 2690
Haven't reread the whole thread again, but this just came to me:
Is the value of the control-variable defined, when you BREAK on the last iteration?

I know we all said, the control-var will retain its defined value when you jump out of the loop with a break, but what about the last iteration?
Code: Pascal  [Select][+][-]
  1. For i:=1 To 10 Do
  2.   Begin
  3.      If i=10 Then Break;
  4.   End;
  5.  
  6. //What's the definite state of i?

Logic tells me, it should be defined (as it has been mentioned many times because i'm breaking out of the loop)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 7616
Haven't reread the whole thread again, but this just came to me:
Is the value of the control-variable defined, when you BREAK on the last iteration?

I believe it should, because the numbers are inclusive. However I'd hate to find myself debating it with one of the "well, it always works for me so it must be defined" crowd :-/

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Kays

  • Hero Member
  • *****
  • Posts: 604
  • Whasup!?
    • KaiBurghardt.de
[…] Is there a way to enable a warning/error/hint so it warns us of this? […]
You may be interested in following issues #12508 and #34248.

[…] The control variable ends up with no defined (i.e. predictable) value. It does not in some way become undeclared, or have a special value assigned to it that code can subsequently recognise as an undefined value. […]
Well, then it is merely a matter of always resetting the “not initialized flag” so a warning is emitted. Although I find it quite useless then, you know, if the control variable’s preceding initialized state is revoked no matter what, then it’s like “warning: program may run successfully”.

[…] While I agree that 11 would have been an more expected result than 10 […]
This may be because you were familiarized with C (or a similar programming language) first, or maybe you are not capable of abstracting from the low‑level implementation details and the semantics of high‑level programming language. For me as a “Pascal native” a value of 11 is unintuitive.

[…]
Code: Pascal  [Select][+][-]
  1. For i:=1 To 10 Do
  2.   Begin
  3.      If i=10 Then Break;
  4.   End;
  5.  
  6. //What's the definite state of i?
[…]
It is defined to be 10.

EDIT: An additional issue has been mentioned and linked.
« Last Edit: August 24, 2024, 08:35:45 pm by Kays »
Yours Sincerely
Kai Burghardt

MarkMLl

  • Hero Member
  • *****
  • Posts: 7616
[…] Is there a way to enable a warning/error/hint so it warns us of this? […]
You may be interested in following issue #34248.

Also see Sven's comment from (much) earlier in the thread, which encouraged me to resubmit it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

DarioGL

  • New member
  • *
  • Posts: 7
Thanks a ton to everybody for their collaborations and help in this matter. As its already reported to be implemented im going to lock the thread to avoid going in circles.

Regards,

 

TinyPortal © 2005-2018