[fpc-pascal] Functions with out parameters
Damien Gerard
milipili at shikami.org
Mon Mar 10 14:19:00 CET 2008
I have the following function :
procedure Foo(const AText: UTF8String; out AValue: UTF8String);
begin
ShowMessage(AText); //
AValue := 'A custom value';
end;
procedure AnotherMethod;
var s: UTF8String;
begin
s := 'Some text';
Foo(s, s);
// Do something with `s`
end;
If you have this, the showmessage display an empty string and
sometimes the app crashes. I had a doubt so I tried this :
procedure AnotherMethod;
var s, t: UTF8String;
begin
s := 'Some text';
t := s;
Foo(t, s);
// Do something with `s`
end;
And it works well the app never crash and all my variables are
correct. So I guess the two parameters share the same pointer. It
seems logic to me in this case.
However, is there a way the compiler produces an error in this case ?
I suppose there is a similar case with the 'var' keyword.
--
Damien Gerard
milipili at shikami.org
"Intelligence is 10 million rules."
-- Douglas Lenat
More information about the fpc-pascal
mailing list