[fpc-devel] Redefine FillChar() to use out parameter instead
Florian Klaempfl
florian at freepascal.org
Thu Nov 19 22:31:34 CET 2009
Vinzent Höfler schrieb:
> Florian Klaempfl <florian at freepascal.org>:
>
>> Vinzent Höfler schrieb:
>>> Florian Klaempfl <florian at freepascal.org>:
>>>
>>>> A VAROUT parameter could have the same semantics as VAR except that the
>>>> compiler does not expect that it is needed that it is initialized. But
>>>> be warned: with such a parameter type you can easily create memory
>> leaks
>>>> with automated types like ansistrings.
>>> I don't understand the issue.
>> Indeed, I noticed :)
>
> Care to explain why the programmer can easily determine something which seems to be an unsolvable problem for the compiler, although both have exactly the same information?
Because a VAROUT parameter would be simply overwritten by the callee
even if it contains a valid automated type:
procedure p(varout v : ansistring);
begin
v:='asdf';
end;
var
s : string;
begin
s:='asdf';
s:=s+s;
p(s); <-- memory leak, s+s is never freed
end;
OUT does prevent this because s is cleaned up before p is entered. OTOH,
OUT causes a crash if s is filled with garbage.
More information about the fpc-devel
mailing list