Home Are constant record's unlisted elements initialized with default values in Delphi?
Reply: 1

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

Bozzy
1#
Bozzy Published in 2018-02-09 10:37:19Z

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.

LU RD
2#
LU RD Reply to 2018-02-09 12:51:34Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311033 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO