[fpc-pascal] var r: record = () initialization

Michael Van Canneyt michael at freepascal.org
Sat May 31 10:37:44 CEST 2008



On Fri, 30 May 2008, L wrote:

> Consider local procedure scope stack records in MODE OBJFPC:
> 
> procedure....
> var
> R1: TSomeRecord = ();
> R2: TSomeRecord;
> 
> procedure....
> var
> O1: TSomeOldObject = ();
> O2: TSomeOldObject;
> 
> In the case of R1 and O1, it seems the record's fields are initialized to
> zero, even though the fields are not specified inside the brackets.
> 
> (R2 and O2 are uninitialized - and are not the same as R1 and O1)
> 
> Is this guaranteed? Can this change in the future or does it differ across
> platforms? This would be a case where modern pascal could really use some
> clearly defined language semantics - specifying the behavior of these
> situations.
> 
> I read in the fpc manual that when the compiler warns that some fields may not
> be initialized, they are still initialized (which is a bit confusing English).
> 
> Quote:
> "Warning: Some fields coming after ”arg1” weren’t initialized
> You can leave some fields at the end of a type constant record uninitialized
> (the compiler will initialize them to zero automatically). This may be the
> cause of subtle problems."
> 
> Regarding "the compiler will initialize them to zero".. this in fact means
> that they *are* initialized to "defaults" and saying they are "not
> initialized" really I guess means not initialized by "the programmer". 

Correct.

> What I
> wish to confirm is the portability of this syntax and whether it does
> initialize fields to zero always, consistently, no matter what platform, etc.

The compiler will initialize to zero explicitly, if a part of the record 
is initialized. This part is portable and guaranteed.

If you just declare a variable, you may not assume anything.
The pascal specs say explicitly that you should not rely on variables 
being initialized.

> It would be nice if it did, because this would mean there is no need for a
> previous "AUTOINIT" feature idea I suggested - it already exists.

You don't need autoinit.

Just do a

var
  Something : SomeType = SomeValue;

Michael.


More information about the fpc-pascal mailing list