[fpc-pascal] Are record fields zeroed on declaration of a record?

Michael Van Canneyt michael at freepascal.org
Thu Oct 8 10:47:52 CEST 2020



On Thu, 8 Oct 2020, Bo Berglund via fpc-pascal wrote:

> I would like to know if data containers of type packed record are
> zeroed when the item is declared?
>
> type
>  TMyRecord = packed record
>    AField1: byte;
>    AField2: word;
>    AField3: single;
>    AField4: boolean;
>    AField5: array[0..15] of Cardinal;
>  end;
>
> procedure SomeProc;
> var
>  myRec: TMyRecord;
> begin
> ...
>
> Will random data fill myRec until I assign each field or will the
> entire record be zeroed at this point?

No, as with all local unmanaged variables it contains random data. 
The best you can do is add this as the first line:

begin
   MyRec:=Default(TMyRecord);

It will in effect zero out the record.

Michael.


More information about the fpc-pascal mailing list