Quantcast

How to solve "variable does not seem to be initialized" compiler hint.

classic Classic list List threaded Threaded
69 messages Options
1234
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to solve "variable does not seem to be initialized" compiler hint.

Graeme Geldenhuys
Hi,

And how do I solve this compiler hint?  Normally I use FillChar(), but
here it made no difference. The hint no simply moved to the line
containing FillChar() call. :-(


/home/graemeg/programming/3rdParty/DCPcrypt/dcpblockciphers.pas(229,16)
Hint: Local variable "Temp" does not seem to be initialized


-------------------------------
procedure TDCP_blockcipher64.DecryptCFB8bit(const Indata; var Outdata;
    Size: longword);
var
  i: longword;
  p1, p2: Pbyte;
  TempByte: byte;
  Temp: array[0..7] of byte;
begin
  if not fInitialized then
    raise EDCP_blockcipher.Create('Cipher not initialized');
  p1:= @Indata;
  p2:= @Outdata;
  FillChar(Temp, SizeOf(Temp), 0); // <-- I added this to no affect
  for i:= 1 to Size do
  begin
    TempByte:= p1^;
    EncryptECB(CV,Temp);          // <- original hint was here
    p2^:= p1^ xor Temp[0];
    Move(CV[1],CV[0],8-1);
    CV[7]:= TempByte;
    Inc(p1);
    Inc(p2);
  end;
end;
-------------------------------



Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

José Mejuto
Hello Graeme,

Tuesday, November 17, 2009, 11:03:22 AM, you wrote:

GG> /home/graemeg/programming/3rdParty/DCPcrypt/dcpblockciphers.pas(229,16)
GG> Hint: Local variable "Temp" does not seem to be initialized
[...]
GG>     raise EDCP_blockcipher.Create('Cipher not initialized');
GG>   p1:= @Indata;
GG>   p2:= @Outdata;

Insert this, or in other words "use it". The reason is that "fillchar"
first parameter is "var" instead "out".

Temp[0]:=0;

GG>   FillChar(Temp, SizeOf(Temp), 0); // <-- I added this to no affect
GG>   for i:= 1 to Size do

--
Best regards,
 JoshyFun

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Fantomas
In reply to this post by Graeme Geldenhuys
Hello, Graeme!

Tuesday, November 17, 2009, 1:03:22 PM, you wrote:

> And how do I solve this compiler hint?  Normally I use FillChar(), but here it
> made no difference. The hint no simply moved to the line containing FillChar()
> call. :-(


> /home/graemeg/programming/3rdParty/DCPcrypt/dcpblockciphers.pas(229,16)
> Hint: Local variable "Temp" does not seem to be initialized


>   p2:= @Outdata;
>   FillChar(Temp, SizeOf(Temp), 0); // <-- I added this to no affect
>   for i:= 1 to Size do
>   begin
>     TempByte:= p1^;
>     EncryptECB(CV,Temp);          // <- original hint was here
>     p2^:= p1^ xor Temp[0];
>     Move(CV[1],CV[0],8-1);
>     CV[7]:= TempByte;


That compiler hint appears because you have not assigned a value to the variable
Temp before passing it to a function as a parameter. You can pay no attention to
that hint or switch off hints in your code where the hint is produced, if it
really bothers you.


--
Best regards,
Fantomas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Graeme Geldenhuys
Fantomas wrote:
>
> That compiler hint appears because you have not assigned a value to the variable
> Temp before passing it to a function as a parameter. You can pay no attention to
> that hint or switch off hints in your code where the hint is produced, if it
> really bothers you.

That you.  I agree with JoshyFun though. It seems like it is actually a
problem in the FillChar() definition and that should be fixed, instead
of simply ignoring the hint.

I raised this issue in the fpc-devel mailing list.

Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re[2]: How to solve "variable does not seem to be initialized" compiler hint.

Fantomas
Hello, Graeme!

Tuesday, November 17, 2009, 2:20:18 PM, you wrote:

> That you.  I agree with JoshyFun though. It seems like it is actually a
> problem in the FillChar() definition and that should be fixed, instead of
> simply ignoring the hint.

> I raised this issue in the fpc-devel mailing list.

In my humble opinion, there are no problems with the FillChar declaration. But
the way you wrote your _absolutely correct_ code produced a hint. You shold use
$HINTS OFF, for amendment to the FillChar declaration seems to be done unlikely.


--
Best regards,
Fantomas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Jürgen Hestermann
> In my humble opinion, there are no problems with the FillChar declaration. But
> the way you wrote your _absolutely correct_ code produced a hint. You shold use
> $HINTS OFF, for amendment to the FillChar declaration seems to be done unlikely.

But there is a reason for the hints. It should point you to possible problems (or optimitations). When switching it off, you also loose all other hints. That's not a solution, it's a workaround.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

cobines
Can the Fill... functions be changed to have the first parameter "out"
instead of "var" ? Surely they don't use it as an input parameter.

--
cobines
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re[2]: How to solve "variable does not seem to be initialized" compiler hint.

Fantomas
In reply to this post by Jürgen Hestermann
>> In my humble opinion, there are no problems with the FillChar declaration.
>> But the way you wrote your _absolutely correct_ code produced a hint. You
>> shold use $HINTS OFF, for amendment to the FillChar declaration seems to be
>> done unlikely.

> But there is a reason for the hints. It should point you to possible problems
> (or optimitations). When switching it off, you also loose all other hints.
> That's not a solution, it's a workaround.

As far as I know $HINTS is a local directive. So, you can circumscribe only a
certain piece of your code. E.g:

{$HINTS OFF}
FillChar(Temp,SizeOf(Temp),0);
{$HINTS ON}

You may consider my point to be a bit conservative, but I really do not think
there is something to change about the FillChar declaration.

--
Best regards,
Fantomas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Jonas Maebe-2
In reply to this post by cobines

On 17 Nov 2009, at 20:47, cobines wrote:

> Can the Fill... functions be changed to have the first parameter "out"
> instead of "var"

No: http://lists.freepascal.org/lists/fpc-devel/2009-November/018532.html


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re[2]: How to solve "variable does not seem to be initialized" compiler hint.

José Mejuto
In reply to this post by cobines
Hello FPC-Pascal,

Tuesday, November 17, 2009, 8:47:03 PM, you wrote:

c> Can the Fill... functions be changed to have the first parameter "out"
c> instead of "var" ? Surely they don't use it as an input parameter.

Write your own "fillchar" like function and inline it, something like:

procedure MyFillChar(out x; count: SizeInt; Value: char); inline;
begin
{$PUSH}
{$HINTS OFF}
  FillChar(x,count,Value);
{$POP}
end;

--
Best regards,
 JoshyFun

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re[2]: How to solve "variable does not seem to be initialized" compiler hint.

etrusco
On Tue, Nov 17, 2009 at 7:19 PM, JoshyFun <[hidden email]> wrote:

> Hello FPC-Pascal,
>
> Tuesday, November 17, 2009, 8:47:03 PM, you wrote:
>
> c> Can the Fill... functions be changed to have the first parameter "out"
> c> instead of "var" ? Surely they don't use it as an input parameter.
>
> Write your own "fillchar" like function and inline it, something like:
>
> procedure MyFillChar(out x; count: SizeInt; Value: char); inline;
> begin
> {$PUSH}
> {$HINTS OFF}
>  FillChar(x,count,Value);
> {$POP}
> end;
>
> --
> Best regards,
>  JoshyFun

Shouldn't this code too raise a warning?

-Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Graeme Geldenhuys
Flávio Etrusco wrote:
>> begin
>> {$PUSH}
>> {$HINTS OFF}
>>  FillChar(x,count,Value);
>> {$POP}
>> end;
>>
>
> Shouldn't this code too raise a warning?

No, that's what the {$HINTS OFF} is used for - hiding the compiler hints
& warnings.



Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Jürgen Hestermann
>>> begin
>>> {$PUSH}
>>> {$HINTS OFF}
>>>  FillChar(x,count,Value);
>>> {$POP}
>>> end;
>> Shouldn't this code too raise a warning?
> No, that's what the {$HINTS OFF} is used for - hiding the compiler hints
> & warnings.

But isn't the warning raised when x is used as a parameter of a function? The problem is, that fillchar is not considered to be an initialization of x. So when using x it is still considered uninitialized.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Jonas Maebe-2

On 18 Nov 2009, at 17:35, Jürgen Hestermann wrote:

> But isn't the warning raised when x is used as a parameter of a function? The problem is, that fillchar is not considered to be an initialization of x. So when using x it is still considered uninitialized.

a) a "var" parameter is considered as initialising a variable (although it also expects input to be initialised already)
b) "Uninitialised" hints/warnings are only given once per variable (even regardless of whether the warning/hint is printed or not, as the state of the variable in the compiler will be changed so that it won't be printed anymore).


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

zaher dirkey
Related to FillChar
In Delphi there is ZeroMemory, i guess it is windows API, can we have
one in FreePascal to use it instead of FillChar.

--
Zaher Dirkey
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Jonas Maebe-2

On 18 Nov 2009, at 17:53, Zaher Dirkey wrote:

> In Delphi there is ZeroMemory, i guess it is windows API, can we have
> one in FreePascal to use it instead of FillChar.

What is the problem with fillchar? (other than that it prints a wrong hint in some cases.

Also note that the difference between hints and warnings is exactly that hints are not guaranteed to be 100% correct or relevant in all situations; if they were, then they would be warnings. Hints are mainly intended to help you scrutinise your code more closely in case there's a problem you can't find, by flagging things that *might* indicate a problem. Getting all code hint free is probably not even possible (or desirable).


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Jonas Maebe-2

On 18 Nov 2009, at 17:58, Jonas Maebe wrote:

> Also note that the difference between hints and warnings is exactly that hints are not guaranteed to be 100% correct or relevant in all situations; if they were, then they would be warnings.

Or notes. In order of usefulness/accuracy, we have errors, warnings, notes and hints.


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

Aleksa Todorovic-3
In reply to this post by Jonas Maebe-2
On Wed, Nov 18, 2009 at 17:58, Jonas Maebe <[hidden email]> wrote:
>
> What is the problem with fillchar? (other than that it prints a wrong hint in some cases.
>
> Also note that the difference between hints and warnings is exactly that hints are not guaranteed to be 100% correct or relevant in all situations; if they were, then they would be warnings. Hints are mainly intended to help you scrutinise your code more closely in case there's a problem you can't find, by flagging things that *might* indicate a problem. Getting all code hint free is probably not even possible (or desirable).
>

You're completely right about that - there is nothing _wrong_ about
fillchar. But if you have fillchar in, say, 20 places in your code,
then you'll have to check all of those 20 places every time you build
your project. And the idea of notices and hints is they are not to be
ignored, but checked, isn't it? :-)

As of myself, I don't really care if there will be new
safe_fillchar_with_no_hints function or not - it is easy to
encapsulate fillchar, as JoshyFun pointed out. Aside from that,
fillchar is unsafe by its nature, so having hint whenever one uses it
is good idea (as a side effect of uninitialized variable in this
case).
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to solve "variable does not seem to be initialized" compiler hint.

zaher dirkey
In reply to this post by Jonas Maebe-2
On Wed, Nov 18, 2009 at 6:58 PM, Jonas Maebe <[hidden email]> wrote:
>
> What is the problem with fillchar? (other than that it prints a wrong hint in some cases.
>

procedure ZeroMemory(Destination: Pointer; Length: DWORD);
begin
  FillChar(Destination^, Length, 0);
end;

There is no var in ZeroMemory so no hint i guess.

--
Zaher Dirkey
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: How to solve "variable does not seem to be initialized" compiler hint.

Cox, Stuart TRAN:EX
In reply to this post by Aleksa Todorovic-3
In all of this discussion, shouldn't the point be that the passing an un-initialized variable is correctly being reported as a possible error (of limited severity)?  

The _proper_ solution is to initialize the variable's contents and then use it.  The alternative is to substitute an alternative procedure, like, say, ZeroMemory or write a clever wrapper procedure.  

One of the features of languages like Pascal is that one's warned of their oversights by the compiler.  The alternative is blindingly visible in products like W-----$ and buffer overruns a-plenty in other apps compiled with a language from the hierarchy derived from a language between B and D.

Stu Cox (his own opinions)
-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Aleksa Todorovic
Sent: Wednesday, November 18, 2009 9:15 AM
To: FPC-Pascal users discussions
Subject: Re: [fpc-pascal] How to solve "variable does not seem to be initialized" compiler hint.

On Wed, Nov 18, 2009 at 17:58, Jonas Maebe <[hidden email]> wrote:
>
> What is the problem with fillchar? (other than that it prints a wrong hint in some cases.
>
> Also note that the difference between hints and warnings is exactly that hints are not guaranteed to be 100% correct or relevant in all situations; if they were, then they would be warnings. Hints are mainly intended to help you scrutinise your code more closely in case there's a problem you can't find, by flagging things that *might* indicate a problem. Getting all code hint free is probably not even possible (or desirable).
>

You're completely right about that - there is nothing _wrong_ about
fillchar. But if you have fillchar in, say, 20 places in your code,
then you'll have to check all of those 20 places every time you build
your project. And the idea of notices and hints is they are not to be
ignored, but checked, isn't it? :-)

As of myself, I don't really care if there will be new
safe_fillchar_with_no_hints function or not - it is easy to
encapsulate fillchar, as JoshyFun pointed out. Aside from that,
fillchar is unsafe by its nature, so having hint whenever one uses it
is good idea (as a side effect of uninitialized variable in this
case).
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
1234
Loading...