Recent

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 6235
  • Compiler Developer
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #45 on: November 03, 2022, 07:24:11 am »
So what is best way to to this?

Just continue to build within Lazarus and simply learn what the messages mean and whether you can safely ignore or disable them.

Code: Pascal  [Select][+][-]
  1.   a: TStringArray =();
  2.   b : string ='';
  3.  

But I thought that was supposed to be implicit for managed types, or is there a mode dependency there?

Yes, it's implicit and the reason why I personally am opposed to this hint cause unlike for the Result variable it definitely makes no difference.

But I thought that was supposed to be implicit for managed types, or is there a mode dependency there?
It depends on two things:
- heap or stack, heap is clean, stack is not (with the default MM)

Implicit initializaton depends on neither of these and happens always.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8515
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #46 on: December 01, 2022, 09:43:58 pm »
Apropos increasing the size of a dynamic array:

Not a power of two but the golden ratio is way more efficient. ~1.618

Why? Not arguing, interested...

...and as a secondary question, is there a standard class that implements an extensible buffer with "append to end" and "remove from start" support?

MarkMLl
« Last Edit: December 01, 2022, 09:51:56 pm by 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

MarkMLl

  • Hero Member
  • *****
  • Posts: 8515
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #47 on: December 25, 2022, 10:54:32 pm »
...and as a secondary question, is there a standard class that implements an extensible buffer with "append to end" and "remove from start" support?

Well, trusting Thaddy's judgement, that was a fun little exercise.

Some of the issues- not all of which I've yet addressed- are that it's desirable to have a "how many bytes can I add?" property, which is dependent on the maximum permitted size which is (original_size * (golden_ratio ^ n)) hence susceptible to rounding errors.

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

Bitbeisser

  • New Member
  • *
  • Posts: 42
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #48 on: November 17, 2023, 07:58:58 pm »
I have been bothered by this warning a lot as well. While I do understand (and appreciate) that there are cases where this is a useful hint, which could potentially lead to bugs in your code, there are also rather benign reasons, for perfectly valid code where this specific warning is rather superfluous.

For example, I have tons of code where procedures/functions start something like this (just a very simple example)

Code: Pascal  [Select][+][-]
  1. Procedure ReadHeader (Var F : File);
  2. Var Buffer : Array [1..512] of Byte;
  3.      BytesRead    : LongInt;
  4. begin
  5.   BlockRead (F, Buffer, 512, BytesRead);
  6.  
This will result in the warning issued for both the Buffer and BytesRead variables, though these variables are assigned some value by the BlockRead procedure, but the compiler here doesn't check on the var return values and hence doesn't consider this a sufficient initialization of those variables (though it is as good as it gets).

So instead of using dummy assignments as some people suggest and thus unnecessarily bloat up the code, I  will be using the {$PUSH} and {$POP} to locally disable the warning.
Code: Pascal  [Select][+][-]
  1. {$PUSH}
  2. {$WARN 5057 OFF}
  3.   BlockRead (F, Buffer, 512, BytesRead);
  4. {$POP}
  5.  


Using the {$WARN 5057 OFF} at the beginning of the file, I do not consider a safe option, as it would indeed mask cases where a later used variable might accidentally not have been properly initialized/a value assigned...

Bitbeisser

  • New Member
  • *
  • Posts: 42
This is exactly what the {%H-} directive does - read the last sentence in my post above.
Thanks, this seems to be the most sensible solution in most cases, rather than cluelessly adding dummy variable assignments in a procedure/function.

Most of the time, where the message is just annoying rather than a real potential problem, is in quite obvious system functions like BlockRead (), at least for the read buffer variable (ignoring the result (bytes read) parameter is a possible hint to check for end-of-file issues) or procedures like FSplit() and the returned Dir, Name and Ext parameters.

Thaddy

  • Hero Member
  • *****
  • Posts: 18515
  • Here stood a man who saw the Elbe and jumped it.
I would not recommend Lazarus macro's if you compile for both Delphi and Lazarus.
These macro's are tight to the editor and wil byte you when using another editor.
That is the reason all my examples on this forum never use Lazarus macro's.
They are not silly, but close. You can't use them in e.g. Geany.
And I never use Geany macro's ...
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018