On 24.03.2018 23:00, Sven Barth via fpc-devel wrote:
Ondrej Pokorny <laza...@kluug.net <mailto:laza...@kluug.net>> schrieb am Sa., 24. März 2018, 20:49:

    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)
    <http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Variables_%28Delphi%29>
    "If
    you do not explicitly initialize a global variable, the compiler
    initializes it to 0."


So, Delphi *does* document it. Okay, in that case the documentation needs to be updated, cause up to now the assumption has been that this is an implementation detail.

It has always been so in Delphi - at least since I learnt it. The Delphi 7 docs (I can't find older docs online) state it as well: http://docs.embarcadero.com/products/rad_studio/cbuilder6/EN/CB6_ObjPascalLangGuide_EN.pdf on page 5-38 "If you don’t explicitly initialize a global variable, the compiler initializes it to 0." (Docs are at http://docs.embarcadero.com/products/rad_studio/ )

    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).

Ondrej
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to