Recent

Author Topic: Hint message "Local variable xx does not seem to be initialized" problem  (Read 17862 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2802
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #15 on: October 28, 2022, 01:55:10 pm »
Yep, you are right, when run in Lazarus, it gives that hint.

Its because SetLength() takes a var parameter rather than an out one (or so I have always believed). Near as I can tell, SetLength does not read the S, so has no need to have it initialized before getting passed it.

That makes it one of those 'old' things WP mentioned further up. Very annoying ...

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

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #16 on: October 28, 2022, 02:00:43 pm »
tt, I suspect Mark answered the wrong question, not spotting how old it was.

My $deity... you're right. So WTF did somebody revive this thread rather than starting a new one with applicable subject line?

...and for that matter the usual comment about giving compiler version etc. applies :-(

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Чебурашка

  • Hero Member
  • *****
  • Posts: 577
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #17 on: October 28, 2022, 02:06:14 pm »
tt, I suspect Mark answered the wrong question, not spotting how old it was.

My $deity... you're right. So WTF did somebody revive this thread rather than starting a new one with applicable subject line?

...and for that matter the usual comment about giving compiler version etc. applies :-(

MarkMLl

Sorry, the problem was exactly the same, but you're right, I should open another thread.

For the stuff of the compiler I fixed myself.

One extra note: among original thread replies there was one citing a certain {$warn 5057 off}. To be noted that now to obtain the same result {$warn 5091 off} must be used, seems Hints numbering has changed since.
« Last Edit: October 28, 2022, 02:09:52 pm by tt »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 577
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #18 on: October 28, 2022, 02:08:49 pm »
Yep, you are right, when run in Lazarus, it gives that hint.

Its because SetLength() takes a var parameter rather than an out one (or so I have always believed). Near as I can tell, SetLength does not read the S, so has no need to have it initialized before getting passed it.

That makes it one of those 'old' things WP mentioned further up. Very annoying ...

Davo

Maybe SetLength wants a var because this code is possible with no memory leak?

Code: Pascal  [Select][+][-]
  1. var
  2.   a: array of integer;
  3. begin
  4.   SetLength(a, 1); // hint :D
  5.   SetLength(a, 10);
  6.   SetLength(a, 1);
  7.  
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Nicole

  • Hero Member
  • *****
  • Posts: 972
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #19 on: October 28, 2022, 02:16:42 pm »
I would find a "workaround" for the hint / warning.
No, not to get rid of it, but to make your code more robust. It happens that quickly, that a function is re-written or a result gives back something you never have thought of.

So try this:
myVar:=0;
myResult(myVar);

I hope, there is not warning, "var has never been used" .....

And then there was a compiler message in Delphi similar like
{warnings off}
Sometimes I wrote it on top of a block, - because of such reasons.

Not sure, if there is such a thing in Lazarus as well.

And: I migrate my Delphi code as well at the moment.

Чебурашка

  • Hero Member
  • *****
  • Posts: 577
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #20 on: October 28, 2022, 02:23:02 pm »
I would find a "workaround" for the hint / warning.
No, not to get rid of it, but to make your code more robust. It happens that quickly, that a function is re-written or a result gives back something you never have thought of.

So try this:
myVar:=0;
myResult(myVar);

I hope, there is not warning, "var has never been used" .....

And then there was a compiler message in Delphi similar like
{warnings off}
Sometimes I wrote it on top of a block, - because of such reasons.

Not sure, if there is such a thing in Lazarus as well.

And: I migrate my Delphi code as well at the moment.

Thanks, but my observation was of course not related to the hint of the uninitialized variables (I consider them the absolute evil), but for the example reported:

Code: Pascal  [Select][+][-]
  1. var
  2.   a: array of integer;
  3. begin
  4.   SetLength(a, 100);
  5.  

where the initialization occurs, and is done by

Code: Pascal  [Select][+][-]
  1.   SetLength(a, 100);
  2.  

(btw according to compiler documentation).
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

dbannon

  • Hero Member
  • *****
  • Posts: 2802
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #21 on: October 28, 2022, 02:37:10 pm »
Maybe SetLength wants a var because this code is possible with no memory leak?

Code: Pascal  [Select][+][-]
  1. var
  2.   a: array of integer;
  3. begin
  4.   SetLength(a, 1); // hint :D
  5.   SetLength(a, 10);
  6.   SetLength(a, 1);
  7.  

No, I don't think so, arrays are cleaned up automatically. Try putting an a := nil between each Setlength(), it generates no memory leaks....

Come on Mark, does Setlength predate the 'out' qualifier ?  Thaddy will chime in soon and he'll yell at us !

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

Чебурашка

  • Hero Member
  • *****
  • Posts: 577
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #22 on: October 28, 2022, 02:42:13 pm »
Maybe SetLength wants a var because this code is possible with no memory leak?

Code: Pascal  [Select][+][-]
  1. var
  2.   a: array of integer;
  3. begin
  4.   SetLength(a, 1); // hint :D
  5.   SetLength(a, 10);
  6.   SetLength(a, 1);
  7.  

No, I don't think so, arrays are cleaned up automatically. Try putting an a := nil between each Setlength(), it generates no memory leaks....

Come on Mark, does Setlength predate the 'out' qualifier ?  Thaddy will chime in soon and he'll yell at us !

Davo

Of course if I put a

Code: Pascal  [Select][+][-]
  1. a := nil;
  2.  

compiler won't complain anymore, but is this the right way to go?

I would expect compiler to say something if I do

Code: Pascal  [Select][+][-]
  1. var
  2.   a: array of Integer;
  3. begin
  4.  a[3] := 10;
  5.  

But not if i do:

Code: Pascal  [Select][+][-]
  1. var
  2.   a: array of Integer;
  3. begin
  4.  SetLength(a, 4);
  5.  a[3] := 10;
  6.  

FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 577
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #23 on: October 28, 2022, 02:51:07 pm »
Uh, btw, I reopened a old VM with debian 10.11, FPC 3.0.4 and Lazarus 2.0.0+dfsg-2 with same syntax type and also verified that these hints are set to be emitted.

This time got not message.

FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Nicole

  • Hero Member
  • *****
  • Posts: 972
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #24 on: October 28, 2022, 02:51:29 pm »
As I could not believe this, I tried it.
And I tried
   for i:= 0 to 99 do
       a:=9;

The same!
hm.

Probably somebody is here for the next update of Lazarus...

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #25 on: October 28, 2022, 02:53:51 pm »
Come on Mark, does Setlength predate the 'out' qualifier ?  Thaddy will chime in soon and he'll yell at us !

As an aside: yes, I think it probably does but it would be interesting if anybody could confirm it either way.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #26 on: October 28, 2022, 03:12:17 pm »
SetLength uses neither var nor out, because it's an intrinsic and works on the first parameter directly.

I have requested multiple times to FPK already to disable that hint for local variables of managed types (not the Result variable however), because those will always be initialized to sane values upon function entry. However he argues that it's just a hint (and by default FPC does not display hints) and if it bothers someone then one can disable it with a {$warn 5091 off}.

Чебурашка

  • Hero Member
  • *****
  • Posts: 577
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #27 on: October 28, 2022, 03:25:00 pm »
(and by default FPC does not display hints)

Using your observation, I was thinking that debian packager changed from 10.11 to 11.5 the default Lazarus setting related to Showing hints (-vh) but is not so, is enabled on both versions.

I don't know what is going on, we should invoke the soul of Niklaus.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #28 on: October 28, 2022, 04:15:35 pm »
I have requested multiple times to FPK already to disable that hint for local variables of managed types (not the Result variable however), because those will always be initialized to sane values upon function entry. However he argues that it's just a hint (and by default FPC does not display hints) and if it bothers someone then one can disable it with a {$warn 5091 off}.

[Grumble **] Well, at least it's not just ordinary users who don't get listened to.

** Intended to be good-natured and hopefully interpreted by all as such.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #29 on: October 28, 2022, 04:49:13 pm »
(and by default FPC does not display hints)

Using your observation, I was thinking that debian packager changed from 10.11 to 11.5 the default Lazarus setting related to Showing hints (-vh) but is not so, is enabled on both versions.

I said FPC, not Lazarus, that's an important difference.

 

TinyPortal © 2005-2018