Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-05

var r: record = () initialization

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

L

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

Michael Van Canneyt
to L
Show quoted text 34 lines
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.