[fpc-devel] Redefine FillChar() to use out parameter instead
klenin at gmail.com
Thu Nov 19 11:21:03 CET 2009
I still do not fully understand.
On Thu, Nov 19, 2009 at 19:20, Martin <lazarus at mfriebe.de> wrote:
> Jonas Maebe wrote:
> Well in this case, the code is actually positively affected by the "out"
> param (because it avoids the mem leak)
Jonas, can you confirm that your example is incorrect one,
and Martin's example below is actually what you meant?
> But if you ever handled ref-counted data by hand.....
> type TStringArray = Array of string;
> procedure InsertOne(a: TStringArray);
> var l : integer;
> l := length(a)
> SetLength(a, l + 1)
> Move(a, a, l*SizeOf(String));
> // now a and a point to the same string
> // but the string has still a ref-count of 1
> // so assigning a := nil => would release the memory used by a
> FillChar(a, SizeOf(String), 0);
> the above works, because Filchar has a var param. otherwise it would
> finalize the string, and a would point to released memory....
Ugh, now this is what I consider to be very ugly.
I did actually wrote exactly this kind of code for Delphi, but it never even
occurred to me to rely on that undocumented distinction between
out and var parameters. I wrote something like
Integer(a) := 0;
So I'd say that this the kind of broken code not worth supporting.
Alexander S. Klenin
More information about the fpc-devel