Warning "Local variable seems to be not initialized" on dyn array

classic Classic list List threaded Threaded
26 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Warning "Local variable seems to be not initialized" on dyn array

Jürgen Hestermann
I get a warning that a local dynamic array variable is not initialized.
Aren't such managed types initialized by default?
On http://wiki.freepascal.org/Dynamic_array it says:

Actually, dynamic arrays are pointers with automatic dereferencing. They are initialized to nil automatically.

So why the warning?

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

Re: Warning "Local variable seems to be not initialized" on dyn array

Le Duc Hieu
From my observation while debugging, local variable inside procedure/functions usually contains garbage value (not initialized).

From: Jürgen Hestermann
Sent: Monday, 9 March 2015 23:30
To: FPC-Pascal users discussions
Reply To: FPC-Pascal users discussions
Subject: [fpc-pascal] Warning "Local variable seems to be not initialized" on dyn array

I get a warning that a local dynamic array variable is not initialized.
Aren't such managed types initialized by default?
On http://wiki.freepascal.org/Dynamic_array it says:

Actually, dynamic arrays are pointers with automatic dereferencing. They are initialized to nil automatically.

So why the warning?


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

Re: Warning "Local variable seems to be not initialized" on dyn array

Jonas Maebe-2
In reply to this post by Jürgen Hestermann

On 09 Mar 2015, at 17:25, Jürgen Hestermann wrote:

> I get a warning that a local dynamic array variable is not  
> initialized.
> Aren't such managed types initialized by default?
> On http://wiki.freepascal.org/Dynamic_array it says:
>
> Actually, dynamic arrays are pointers with automatic dereferencing.  
> They are initialized to *nil* automatically.
>
> So why the warning?

http://bugs.freepascal.org/view.php?id=24601#c75617 (and the comment  
below + use -vq to see warning numbers, and -vmXXXX to block a  
particular warning number).


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

Re: Warning "Local variable seems to be not initialized" on dyn array

Jürgen Hestermann

Am 2015-03-09 um 17:40 schrieb Jonas Maebe:

>
> On 09 Mar 2015, at 17:25, Jürgen Hestermann wrote:
>
>> I get a warning that a local dynamic array variable is not initialized.
>> Aren't such managed types initialized by default?
>> On http://wiki.freepascal.org/Dynamic_array it says:
>>
>> Actually, dynamic arrays are pointers with automatic dereferencing. They are initialized to *nil* automatically.
>>
>> So why the warning?
>
> http://bugs.freepascal.org/view.php?id=24601#c75617 (and the comment below + use -vq to see warning numbers, and -vmXXXX to block a particular warning number).
>
>

Thanks for the answer.
But still: Why is there such a warning for managed types at all?
Dynamic arrays are initialized by default so the warning text is simply wrong.
It IS initialized and the compiler knows this.

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

Re: Warning "Local variable seems to be not initialized" on dyn array

Jonas Maebe-2


On 09 Mar 2015, at 17:49, Jürgen Hestermann <[hidden email]> wrote:

> Am 2015-03-09 um 17:40 schrieb Jonas Maebe:
>>
>> http://bugs.freepascal.org/view.php?id=24601#c75617 (and the comment below + use -vq to see warning numbers, and -vmXXXX to block a particular warning number).
>
> Thanks for the answer.
> But still: Why is there such a warning for managed types at all?

That is the same question you asked in your original mail and which is answered in the bug report comment I linked in reply.


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

Re: Warning "Local variable seems to be not initialized" on dyn array

Jürgen Hestermann

Am 2015-03-09 um 18:31 schrieb Jonas Maebe:

On 09 Mar 2015, at 17:49, Jürgen Hestermann [hidden email] wrote:

Am 2015-03-09 um 17:40 schrieb Jonas Maebe:
http://bugs.freepascal.org/view.php?id=24601#c75617 (and the comment below + use -vq to see warning numbers, and -vmXXXX to block a particular warning number).
Thanks for the answer.
But still: Why is there such a warning for managed types at all?
That is the same question you asked in your original mail and which is answered in the bug report comment I linked in reply.


Not quite. In the bug comment you wrote:

The warning is by design for all kinds of variables and regardless of their storage location (as noted by the original submitter, global/static variables of any kind are always initialised by 0/nil/...). The warning means that no value has been explicitly assigned to these variables (which may indicate a logic error in the code), not necessarily that they contain an unpredictable value.

But still there is the question *why* this design exists?
I think it is a bad design.
It should know that managed types *are* initialized and not raise a warning in this case.
Why generate a warning with an incorrect statement?
Am I forced to add (superfluous) initialization code or somehow suppress this kind of
warning just to get rid of this false warnings which are useless from the beginning?


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

Re: Warning "Local variable seems to be not initialized" on dyn array

DaWorm
I would think this part answers that question:  "The warning means that no value has been explicitly assigned to these variables (which may indicate a logic error in the code), not necessarily that they contain an unpredictable value." (emphasis mine)

Jeff.



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

Re: Warning "Local variable seems to be not initialized" on dyn array

Jürgen Hestermann

Am 2015-03-09 um 19:24 schrieb DaWorm:
I would think this part answers that question:  "The warning means that no value has been explicitly assigned to these variables (which may indicate a logic error in the code), not necessarily that they contain an unpredictable value." (emphasis mine)


No, it answer not the question.
As I already wrote in my other mail:
Why am I forced to add superfluous (initialization) code
just to get rid of a warning that is completely useless (false).
Managed types *are* initialized so this warning just pollutes the
message window and distracts from the realy useful information.

Why not simply omit these messages (for managed types)?


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

Re: Warning "Local variable seems to be not initialized" on dyn array

el_es
On 10/03/15 06:10, Jürgen Hestermann wrote:

>
> Am 2015-03-09 um 19:24 schrieb DaWorm:
>> I would think this part answers that question:  "The warning means
>> that no value has been */explicitly/* assigned to these variables
>> (which */may indicate a logic error/* in the code), not necessarily
>> that they contain an unpredictable value." (emphasis mine)
>>
>>
> No, it answer not the question. As I already wrote in my other mail:
> Why am I forced to add superfluous (initialization) code just to get
> rid of a warning that is completely useless (false). Managed types
> *are* initialized so this warning just pollutes the message window
> and distracts from the realy useful information.
>
> Why not simply omit these messages (for managed types)?
>
>

Just because YOU don't need it, doesn't mean it's useless for everybody...

http://www.freepascal.org/docs-html/prog/progsu80.html#x87-860001.2.80

why not just use this to suppress the warning JUST where you DO know it's not needed?

(you can suppress warning by number too IIUC).


el es


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

Re: Warning "Local variable seems to be not initialized" on dyn array

Mattias Gaertner
On Tue, 10 Mar 2015 08:48:49 +0000
Lukasz Sokol <[hidden email]> wrote:

> On 10/03/15 06:10, Jürgen Hestermann wrote:
>[...]
> > Why not simply omit these messages (for managed types)?
>
> Just because YOU don't need it, doesn't mean it's useless for everybody...
>
> http://www.freepascal.org/docs-html/prog/progsu80.html#x87-860001.2.80
>
> why not just use this to suppress the warning JUST where you DO know it's not needed?
>
> (you can suppress warning by number too IIUC).

Yes.
For example in Lazarus 1.3+ you can right click the message and
click on "Hide with project option (-vm5089)".

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Warning "Local variable seems to be not initialized" on dyn array

el_es
On 10/03/15 09:08, Mattias Gaertner wrote:

> On Tue, 10 Mar 2015 08:48:49 +0000
> Lukasz Sokol <[hidden email]> wrote:
>
>> On 10/03/15 06:10, Jürgen Hestermann wrote:
>> [...]
>>> Why not simply omit these messages (for managed types)?
>>
>> Just because YOU don't need it, doesn't mean it's useless for everybody...
>>
>> http://www.freepascal.org/docs-html/prog/progsu80.html#x87-860001.2.80
>>
>> why not just use this to suppress the warning JUST where you DO know it's not needed?
>>
>> (you can suppress warning by number too IIUC).
>
> Yes.
> For example in Lazarus 1.3+ you can right click the message and
> click on "Hide with project option (-vm5089)".

Cool, but probably not what I meant ;) as this hides it project-wide ?

I mean, it's good to have this warning enabled by default and only suppress it around code
that /I/ know it's bogus at. Otherwise, it /really/ wouldn't serve its purpose of pointing
where a PEBKAC^WLOGIC ERROR may occur...

Sort of like surround the variable declaration with
{$WARN IDENTIFIER OFF}  
{$WARN IDENTIFIER ON}  explicitly (then have code tools fold it so it won't pinch our eyes ;) )

>
> Mattias


el es

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

Re: Warning "Local variable seems to be not initialized" on dyn array

Mattias Gaertner
On Tue, 10 Mar 2015 12:04:37 +0000
Lukasz Sokol <[hidden email]> wrote:

>[...]
> > For example in Lazarus 1.3+ you can right click the message and
> > click on "Hide with project option (-vm5089)".
>
> Cool, but probably not what I meant ;) as this hides it project-wide ?

Yes. I guess that is what Jürgen wants.

I agree with you that in general the hint is good and should be
handled depending on each occurrence.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Warning "Local variable seems to be not initialized" on dyn array

Sven Barth-2
In reply to this post by el_es

Am 10.03.2015 13:05 schrieb "Lukasz Sokol" <[hidden email]>:
>
> On 10/03/15 09:08, Mattias Gaertner wrote:
> > On Tue, 10 Mar 2015 08:48:49 +0000
> > Lukasz Sokol <[hidden email]> wrote:
> >
> >> On 10/03/15 06:10, Jürgen Hestermann wrote:
> >> [...]
> >>> Why not simply omit these messages (for managed types)?
> >>
> >> Just because YOU don't need it, doesn't mean it's useless for everybody...
> >>
> >> http://www.freepascal.org/docs-html/prog/progsu80.html#x87-860001.2.80
> >>
> >> why not just use this to suppress the warning JUST where you DO know it's not needed?
> >>
> >> (you can suppress warning by number too IIUC).
> >
> > Yes.
> > For example in Lazarus 1.3+ you can right click the message and
> > click on "Hide with project option (-vm5089)".
>
> Cool, but probably not what I meant ;) as this hides it project-wide ?
>
> I mean, it's good to have this warning enabled by default and only suppress it around code
> that /I/ know it's bogus at. Otherwise, it /really/ wouldn't serve its purpose of pointing
> where a PEBKAC^WLOGIC ERROR may occur...
>
> Sort of like surround the variable declaration with
> {$WARN IDENTIFIER OFF}
> {$WARN IDENTIFIER ON}  explicitly (then have code tools fold it so it won't pinch our eyes ;) )

Note: you should use the sequence
{$push}{$warn xyz off}bla{$pop} as otherwise you would enable the warning if it was disabled from the command line parameters or the configuration.

Regards,
Sven


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

Re: Warning "Local variable seems to be not initialized" on dyn array

Jürgen Hestermann
In reply to this post by el_es

Am 2015-03-10 um 09:48 schrieb Lukasz Sokol:

> On 10/03/15 06:10, Jürgen Hestermann wrote:
>> Am 2015-03-09 um 19:24 schrieb DaWorm:
>>> I would think this part answers that question:  "The warning means
>>> that no value has been */explicitly/* assigned to these variables
>>> (which */may indicate a logic error/* in the code), not necessarily
>>> that they contain an unpredictable value." (emphasis mine)
>>>
>>>
>> No, it answer not the question. As I already wrote in my other mail:
>> Why am I forced to add superfluous (initialization) code just to get
>> rid of a warning that is completely useless (false). Managed types
>> *are* initialized so this warning just pollutes the message window
>> and distracts from the realy useful information.
>>
>> Why not simply omit these messages (for managed types)?
>>
>>
> Just because YOU don't need it, doesn't mean it's useless for everybody...
>
>
Then please tell me, what do you (or anybody else) gain from this warning?
Especially, as it claims something that is definitely not true (for managed types).
It may even misguide some to add initialisation code without need and
it distracts from the realy important messages.

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

Re: Warning "Local variable seems to be not initialized" on dyn array

Jürgen Hestermann
In reply to this post by Mattias Gaertner

Am 2015-03-10 um 10:08 schrieb Mattias Gaertner:

> On Tue, 10 Mar 2015 08:48:49 +0000
> Lukasz Sokol <[hidden email]> wrote:
>
>> On 10/03/15 06:10, Jürgen Hestermann wrote:
>> [...]
>>> Why not simply omit these messages (for managed types)?
>> Just because YOU don't need it, doesn't mean it's useless for everybody...
>>
>> http://www.freepascal.org/docs-html/prog/progsu80.html#x87-860001.2.80
>>
>> why not just use this to suppress the warning JUST where you DO know it's not needed?
>>
>> (you can suppress warning by number too IIUC).
> Yes.
> For example in Lazarus 1.3+ you can right click the message and
> click on "Hide with project option (-vm5089)".
>
Would this oppress these warnings for managed types only?
Or would I loose them for non-managed types too?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Warning "Local variable seems to be not initialized" on dyn array

Jonas Maebe-2

On 10 Mar 2015, at 17:24, Jürgen Hestermann wrote:

> Would this oppress these warnings for managed types only?
> Or would I loose them for non-managed types too?

Let me repeat myself from the very first reply I sent to you in this  
thread: "http://bugs.freepascal.org/view.php?id=24601#c75617 (and the  
comment below + use -vq to see warning numbers, and -vmXXXX to block a  
particular warning number)."

See in particular the referenced "comment below".


Jonas

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

Re: Warning "Local variable seems to be not initialized" on dyn array

Sven Barth-2
In reply to this post by Jürgen Hestermann

Am 10.03.2015 17:25 schrieb "Jürgen Hestermann" <[hidden email]>:
>
>
> Am 2015-03-10 um 10:08 schrieb Mattias Gaertner:
>
>> On Tue, 10 Mar 2015 08:48:49 +0000
>> Lukasz Sokol <[hidden email]> wrote:
>>
>>> On 10/03/15 06:10, Jürgen Hestermann wrote:
>>> [...]
>>>>
>>>> Why not simply omit these messages (for managed types)?
>>>
>>> Just because YOU don't need it, doesn't mean it's useless for everybody...
>>>
>>> http://www.freepascal.org/docs-html/prog/progsu80.html#x87-860001.2.80
>>>
>>> why not just use this to suppress the warning JUST where you DO know it's not needed?
>>>
>>> (you can suppress warning by number too IIUC).
>>
>> Yes.
>> For example in Lazarus 1.3+ you can right click the message and
>> click on "Hide with project option (-vm5089)".
>>
> Would this oppress these warnings for managed types only?
> Or would I loose them for non-managed types too?

It mutes the warning with the ID 5089 (the ID can be seen with -vq). So as long as the managed and unmanaged ones use the same warning ID (AFAIR they do) they will both be silenced.

Regards,
Sven


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

Re: Warning "Local variable seems to be not initialized" on dyn array

Sven Barth-2

Am 10.03.2015 17:36 schrieb "Sven Barth" <[hidden email]>:
>
> Am 10.03.2015 17:25 schrieb "Jürgen Hestermann" <[hidden email]>:
>
> >
> >
> > Am 2015-03-10 um 10:08 schrieb Mattias Gaertner:
> >
> >> On Tue, 10 Mar 2015 08:48:49 +0000
> >> Lukasz Sokol <[hidden email]> wrote:
> >>
> >>> On 10/03/15 06:10, Jürgen Hestermann wrote:
> >>> [...]
> >>>>
> >>>> Why not simply omit these messages (for managed types)?
> >>>
> >>> Just because YOU don't need it, doesn't mean it's useless for everybody...
> >>>
> >>> http://www.freepascal.org/docs-html/prog/progsu80.html#x87-860001.2.80
> >>>
> >>> why not just use this to suppress the warning JUST where you DO know it's not needed?
> >>>
> >>> (you can suppress warning by number too IIUC).
> >>
> >> Yes.
> >> For example in Lazarus 1.3+ you can right click the message and
> >> click on "Hide with project option (-vm5089)".
> >>
> > Would this oppress these warnings for managed types only?
> > Or would I loose them for non-managed types too?
>
> It mutes the warning with the ID 5089 (the ID can be seen with -vq). So as long as the managed and unmanaged ones use the same warning ID (AFAIR they do) they will both be silenced.

*looks at the comments of the bug report again*
Ups, no, they are different. So at least if you use a current 2.7.1 or newer only the managed ones will be hidden.

Regards,
Sven


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

Re: Warning "Local variable seems to be not initialized" on dyn array

Jürgen Hestermann
Am 2015-03-10 um 17:40 schrieb Sven Barth:
 > > It mutes the warning with the ID 5089 (the ID can be seen with -vq). So as long as the managed and unmanaged ones use the same warning ID (AFAIR they do) they will both be silenced.
 > Ups, no, they are different. So at least if you use a current 2.7.1 or newer only the managed ones will be hidden.

Currently I use Lazarus 1.4RC1 with FPC Version 2.6.4.
Does this version already have different IDs for managed and non-managed types?


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

Re: Warning "Local variable seems to be not initialized" on dyn array

el_es
In reply to this post by Jürgen Hestermann
On 10/03/15 16:08, Jürgen Hestermann wrote:

>
> Am 2015-03-10 um 09:48 schrieb Lukasz Sokol:
>> On 10/03/15 06:10, Jürgen Hestermann wrote:
>>> Am 2015-03-09 um 19:24 schrieb DaWorm:
>>>> I would think this part answers that question:  "The warning means
>>>> that no value has been */explicitly/* assigned to these variables
>>>> (which */may indicate a logic error/* in the code), not necessarily
>>>> that they contain an unpredictable value." (emphasis mine)
>>>>
>>>>
>>> No, it answer not the question. As I already wrote in my other mail:
>>> Why am I forced to add superfluous (initialization) code just to get
>>> rid of a warning that is completely useless (false). Managed types
>>> *are* initialized so this warning just pollutes the message window
>>> and distracts from the realy useful information.
>>>
>>> Why not simply omit these messages (for managed types)?
>>>
>>>
>> Just because YOU don't need it, doesn't mean it's useless for everybody...
>>
>>
> Then please tell me, what do you (or anybody else) gain from this warning?
> Especially, as it claims something that is definitely not true (for managed types).
> It may even misguide some to add initialisation code without need and
> it distracts from the realy important messages.
>

Just googled this out:

http://lazarusroad.blogspot.co.uk/2014/02/using-tcomponent-with-automatic.html

and this immediately after:

http://lazarusroad.blogspot.co.uk/2012/07/cost-to-supress-warning-and-how-not-pay.html

el es




_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12