Recent

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

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
Delphi seems to adhere to the same scoping rules as C, which might make me reconsider my opinion about the usefullness of inline vars:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$IFDEF FPC}{$ERROR Won't compile in FreePascal}{$ENDIF}
  3. begin
  4.   for var i:integer := 0 to 9 do
  5.     writeln(i);
  6.   writeln(i); // <-- compile time error
  7.   readln;
  8. end.
Delphi 12.1 CE used for testing.
The above code is equivalent to my first example in C in my previous post.
« Last Edit: August 10, 2024, 04:07:48 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4644
My first example is basically how FreePascal interprets a loop variable, only it does not err.
No, it is not.

Free Pascal interprets the loop variable as in the _second_ example.  That's easy to prove, if it interpreted it as in the first example then using the control variable after the "for" has ended would _never_ be allowed since the control variable would no longer be defined. The variable only exists in the loop not outside of it.

The fact that, in Pascal, the control variable is still defined after the loop ends demonstrates that FPC is interpreting/viewing the situation as in the _second_ example (which is obvious since Pascal does not allow inline declaration, i.e, your first example.)  In the second example and in C, the value of the control variable is always defined.  This is unlike in Pascal where the value of the control variable may or may not be defined depending on how the loop was exited.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

alpine

  • Hero Member
  • *****
  • Posts: 1267
@Thaddy
You keep insisting it's a matter of variable scope but it's not, at least for FPC.

Furthermore, it is inappropriate to reference the C language since the:
Code: C  [Select][+][-]
  1. for(stat1; stat2; stat3) body;
is direct equivalent to:
Code: C  [Select][+][-]
  1. stat1;
  2. while(stat2)
  3. {
  4.   body;
  5.   stat3;
  6. }
In Pascal it is something different by definition. It has nothing to do with the inline variables.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
Well, then you do not fully understand my last two posts.
The C examples are about how a C loop should have been written and how easy it is to err on its consequences, merely to show 440bx he is wrong in that particular statement of his.
He knows that.
« Last Edit: August 10, 2024, 04:50:31 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

alpine

  • Hero Member
  • *****
  • Posts: 1267
Well, then you do not fully understand my last two posts.
The C examples are about how a C loop should have been written and how easy it is to err on its consequences, merely to show 440bx he is wrong in that particular statement of his.
He knows that.
Quote from: 440bx
I guess the problem is that someone who is new to Pascal may believe that using the "for" control variable after the loop ends is ok (as it is in C) even though it _may_ not be and, the compiler currently does not warn the programmer about it.
He is wrong mainly because there is no such thing as "control variable" in C.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
(as it is in C)
is not the case. That is my point. A bold statement which is wrong, with proof.
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4644
Unbelievable!
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
My first example is basically how FreePascal interprets a loop variable, only it does not err.
No, it is not.
Yes it is, that's why the ISO standard says it is undefined. FPC should do better and throw at least a warning like Delphi does, or, ahum, introduce inline vars after all, hi, hi.
BTW, anything about my C skills.... :-X :-\ That was fatal. 8) Own goal.
(I ignore the rest of the contributors on purpose, except TRon and Mark)
« Last Edit: August 10, 2024, 07:12:24 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7859
Yes it is, that's why the ISO standard says it is undefined. FPC should do better and throw at least a warning like Delphi does, or, ahum, introduce inline vars after all, hi,

Agreed both points.

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

tetrastes

  • Hero Member
  • *****
  • Posts: 567
Code: C  [Select][+][-]
  1. for(stat1; stat2; stat3) body;
is direct equivalent to:
Code: C  [Select][+][-]
  1. stat1;
  2. while(stat2)
  3. {
  4.   body;
  5.   stat3;
  6. }

Try to compile this:
Code: C  [Select][+][-]
  1. void main()
  2. {
  3.         for(;;);
  4.                
  5.         while();
  6. }
:D

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
Yes I admit that I wrote zip bombs in my time... That is even more fun... ;D ;) ;) ;) ;)
DON'T show how to do that....I mention this out of context and pure for historical reasons, of course.
« Last Edit: August 10, 2024, 10:16:40 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4644
It's not only unbelievable, it's sad too.... oh well.

let's see..

He is wrong mainly because there is no such thing as "control variable" in C.
No such thing as a "control variable" in C ? really ?... let's see...

https://icarus.cs.weber.edu/~dab/cs1410/textbook/3.Control/for.html
quote from the link above:
Quote
The behavior of for-loops, with some minor exceptions described below, is determined by a loop control variable. (It's possible but rare for traditional for-loops to have multiple control variables.) The variable has an initial and a terminal value, and it (usually) takes on a different value during each iteration of the loop. The next figure illustrates loop control variables and describes their scope.
and there are thousands like that all over the net... maybe you should inform them there is no for loop "control variable" in C.  I'm sure they will appreciate the "enlightment".

If by any chance you doubt there is a for loop control variable in Pascal, here is a link to FPC's for loop documentation where the words "control variable" appears again and again.   I'll give you the benefit of the doubt on that one (that you knew there is a control variable in a Pascal for loop)
https://www.freepascal.org/docs-html/current/ref/refsu59.html




@Thaddy,

You're having another "boolean operator precedence" moment.  Read the links I posted as many times as necessary until you understand their meaning.  (hopefully the pixels won't be worn out by then.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

dbannon

  • Hero Member
  • *****
  • Posts: 3147
    • tomboy-ng, a rewrite of the classic Tomboy
......
But the fact of the matter is that for and its anomalous behaviour is defined in the language ** , cannot be easily removed, and results in behaviour that the compiler cannot easily warn (or error) as being unsafe.
......
i.e. despite its unfortunate behaviour it cannot be quietly dropped from the language implementation.

Sorry Mark, that does not make sense. The anomaly under discussion here is the value of a "for loop" control variable is some times undefined after loop termination ?  Look at what existing code might do if the compiler is "fixed" so that the var contains last value in the loop -
  • Most existing code does NOT use the value of the for loop control variable after the loop terminates, so, the change has no effect.
  • Some (buggy ?) code may try to use the value after the loop. Wow, it will suddenly work as expected. It could not have behaved predictably before so cannot be seen as "depending" on the value being undefined. Unless, maybe, its a random Number Generator ?

There are no other possibilities !

Further, assuming FPC is not changed to retain the value, having the compiler warn of the danger should be little different to how the compiler already warns if an uninitialized variable is used.

Lets be honest, a compiler is just another software tool. The rule is, it should behave as the user expects. So, either retain the variable's value or issue a warning.

Additionally, allow an in-line var declaration (yes, as C does) in a "for loop". Sure St. Nick may turn over in his grave but there are heaps of other far more significant changes that have been made that seem to be regarded as a good thing.

Thaddy
My second example is what most people "expect", but is extremely sloppy C with total disregard for proper scoping rules. ( Hey, It is C..!)
Thaddy, how can you get stuck into a C programmer for not using a construct that is, apparently, permanently denied to us Pascal users ?

 :-*

Thaddy, are you saying that this code will compile under  Delphi 12.1 CE ?
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$IFDEF FPC}{$ERROR Won't compile in FreePascal}{$ENDIF}
  3. begin
  4.   for var i:integer := 0 to 9 do
  5.     writeln(i);
  6.    ....
If so, what is the argument all about ?  We see endless changes made with no other criteria than "Delphi Compatibility".

Davo




Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

silvercoder70

  • Jr. Member
  • **
  • Posts: 73
    • Tim Coates
@Davo,

that code works perfectly under Delphi 11 or 12CE.

(Not to) @Davo,

If you were programming in C in the early 1990s (like me) you would have used the sloppy version of code. It's not ugly, it's just the way it was.
P Plate on FPC | YouTube - https://www.youtube.com/@silvercoder70

alpine

  • Hero Member
  • *****
  • Posts: 1267
*snip*
and there are thousands like that all over the net... maybe you should inform them there is no for loop "control variable" in C.  I'm sure they will appreciate the "enlightment".
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf, ref 6.8.5.3, page 111, at the top. Where is the variable there? Or may be it is a control expression?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018