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

L L at z505.com
Sat May 31 01:10:20 CEST 2008

Consider local procedure scope stack records in MODE OBJFPC:

R1: TSomeRecord = ();
R2: TSomeRecord;

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

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

More information about the fpc-pascal mailing list