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

Michael Van Canneyt michael at freepascal.org
Thu Oct 8 13:04:43 CEST 2020



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

> On Thu, 8 Oct 2020 10:47:52 +0200 (CEST), Michael Van Canneyt via
> fpc-pascal
> <fpc-pascal at lists.freepascal.org> wrote:
>
>>
>>
>> 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.
>>
>
> OK,
> is it possible to define the TMyRecord with default values so that
> when I do Default(TMyRecord) it will be non-zero defaults written
> instead of zeros?

No. You will always need code for that.

Michael.


More information about the fpc-pascal mailing list