Bozzy
Bozzy

Reputation: 611

Are constant record's unlisted elements initialized with default values in Delphi?

Let's suppose these declarations:

type
  TMyRec = record
    Name    : String;
    Age     : Integer;
    Married : Boolean;
  end;

  TMyRecArray = Array[0..3] of TMyRec;

const
  RecArray: TMyRecArray = ((Name: 'John' ; Age: 25; Married: False),
                           (Name: 'Wendy'; Age: 32                ),
                           (Name: 'Nick' ;          Married: True ),
                           (               Age: 19; Married: False));

Are the unlisted record elements in the last three array rows auto-initialized with default values? Or do (can) they contain random data?

Embarcadero's docwiki doesn't say anything official on this.

Upvotes: 4

Views: 157

Answers (1)

LU RD
LU RD

Reputation: 34919

Your record array constant declaration with default values can be declared as:

const
  RecArray: TMyRecArray = ((),
                           (),
                           (),
                           ());

So yes, omitting record fields in the constant declaration will produce default values.


Unfortunately, this behavior is undocumented. You will have to use the debugger to verify. The Delphi predecessor, Turbo Pascal, worked almost the same way. You had to include values for all fields up to the last non-default field.

Upvotes: 5

Related Questions