<p>Am 08.12.2016 05:14 schrieb "Lars" <<a href="mailto:noreply@z505.com">noreply@z505.com</a>>:<br>
><br>
> On Mon, December 5, 2016 4:46 am, Dennis Poon wrote:<br>
> ><br>
><br>
> ><br>
> > Lars wrote:<br>
> ><br>
> >> Do you know about this feature:<br>
> >><br>
> >><br>
> >> var SomeRec: TSomeRecord = ();<br>
> >><br>
> > This is good for global variable.<br>
> > But I wonder whether it has effect in procedure's local variable since<br>
> > it requires the compiler to clear the record on the stack (which is at<br>
> > different location every time).<br>
><br>
> I tested it and it seems to compile, and then the value is in fact 0 when<br>
> I try to access it and do a showmessage(inttostr(rec.i))... where rec.i is<br>
> an integer...<br>
><br>
> But just because it compiles and just because it is zero this time, does<br>
> not mean.. it is consistent. So this needs to be confirmed.</p>
<p>I wouldn't trust the initialization of missing fields (after all the compiler warns about it) as it's undocumented/undefined behavior.</p>
<p>><br>
> It is weird that fpc allows a local initialized var to compile, but delphi<br>
> 5 rejects it...  Delphi 5 is older and may have not had as advanced stack<br>
> technology as fpc ;-)</p>
<p>It's a feature of FPC. It simply adds an approbiate assignment at the entry of the function.</p>
<p>> Another trick is to setup a default global, and then just assign the local<br>
> var to the global (copying the contents) which resets the local to the<br>
> global.. i.e. if you assign one global record () default 0 contents, to a<br>
> local... using:<br>
><br>
> var<br>
>   GlobalDefault := TSomeRec = ();<br>
><br>
> procedure ...<br>
> var<br>
>   local: TSomeRec = ();<br>
> begin<br>
>   localrec := GlobalDefault</p>
<p>Just use Default(TSomeRec) and be done with it... -.-</p>
<p>Regards,<br>
Sven</p>