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

Ondrej Pokorny lazarus at kluug.net
Sat Mar 24 20:49:16 CET 2018


Due to the "Multiple variable initialization" thread I took a look into 
FPC documentation: https://www.freepascal.org/docs-html/ref/refse24.html

There it says:

By default, simple variables in Pascal are not initialized after their 
declaration. Any assumption that they contain 0 or any other default 
value is erroneous: They can contain rubbish.

This is not correct. Global simple variables are always initialized. At 
least in Delphi it is so: 
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Variables_(Delphi) "If 
you do not explicitly initialize a global variable, the compiler 
initializes it to 0."

-----

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'?

function TestB: string;
var B: string;
begin
   B := B + 'a';
   Result := B;
end;

Until now I though that the above code is unsafe and I always 
initialized all local variables (also managed) before use. I assumed the 
initialization to empty string does not need to happen at every routine 
call.

Ondrej




More information about the fpc-devel mailing list