[fpc-devel] Wrong docs: not initialized global variables

Ondrej Pokorny lazarus at kluug.net
Sat Jul 7 08:05:48 CEST 2018


On 03.07.2018 22:08, Florian Klämpfl wrote:
> Am 03.07.2018 um 21:30 schrieb Ondrej Pokorny:
>> On 03.07.2018 20:54, Florian Klämpfl wrote:
>> program Project1;
>> type
>>    TMyArray = array[0..10] of Integer;
>>    procedure Init2(var A: TMyArray);
>>    begin
>>      A[2] := 1;
>>    end;
>> var
>>    A: TMyArray;
>> begin
>>    Init2(A);
>> end.
>>
>> should not emit the warning because A is not being read in Init2.
>
> (please note it throws only a hint in this case as A *is* initialized 
> as it is a global variable).

You are wrong in both of the 2 assumptions you made in your statement:

*1.) Assumption 1: a warning is thrown when A is not a global variable. 
Wrong.*

program Project1;
type
    TMyArray = array[0..10] of Integer;
    procedure Init2(var A: TMyArray);
    begin
      A[2] := 1;
    end;
   procedure Test;
   var
     A: TMyArray;
   begin
     Init2(A); // << project1.lpr(12,12) Hint: Local variable "A" does 
not seem to be initialized
   end;
begin
   Test;
end.

*2.) Assumption 2: A is not initialized if is a not a global variable. 
Wrong.*

See the very first email in this thread and Sven Barth's reply:

On 24.03.2018 23:00, Sven Barth via fpc-devel wrote:
> Ondrej Pokorny <lazarus at kluug.net <mailto:lazarus at kluug.net>> schrieb 
> am Sa., 24. März 2018, 20:49:
>
>     Another question about this sentence in FPC docs: "Managed types are
>     always initialized: in general this means setting the reference
>     count to
>     zero, or setting the pointer value of the type to Nil."
>
>     Does it mean I can assume a local string variable is always (=whenever
>     the routine is called) initialized to ''? I.e. that TestB always
>     returns
>     'a'?
>
>
> For managed types this is indeed guaranteed. Otherwise there'd be 
> exceptions upon the first use of the variable as the RTL would assume 
> that the value is valid.
>
> Regards,
> Sven

Reference in your FPC docs:
https://www.freepascal.org/docs-html/ref/refse24.html

"Managed types are always initialized: in general this means setting the 
reference count to zero, or setting the pointer value of the type to Nil."

-----

The fact that even you as the very first FPC developer are confused by 
the warning/hint and initialization/noinitialization documentation mess 
makes me feel I have been always right in this thread...

Yet all my arguments to clear this mess up were refused with the 
statement that there is no reason to change anything in FPC.

Ondrej
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180707/0fc57ce8/attachment.html>


More information about the fpc-devel mailing list