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

Ondrej Pokorny lazarus at kluug.net
Sun Mar 25 00:09:46 CET 2018


On 24.03.2018 23:33, Ondrej Pokorny wrote:
>>
>>     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.
>
> OK, thanks. My assumption was that setting local managed types to nil 
> is guaranteed only once at the first routine call. And that it is an 
> implementation detail if it is niled at the second call or reused from 
> the first call.
>
> I probably mixed it up with the Result variable (what Maciej wrote about).

I found the source of my assumption, I get a compiler warning in this case:

program StringsTest;
function Test: string;
var S: string;
begin
   S := S + 'a';
   Result := S;
end;
begin
   Writeln(Test);
end.

Emits:
StringsTest.lpr(5,8) Warning: Local variable "S" of a managed type does 
not seem to be initialized

Why do I get the compiler warning when the variable is indeed guaranteed 
to be initialized to nil/empty string?

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


More information about the fpc-devel mailing list