[fpc-pascal] reference-counted function results not initialized to nil
Jürgen Hestermann
juergen.hestermann at gmx.de
Sun Jun 26 18:59:20 CEST 2016
Am 2016-06-26 um 14:27 schrieb Karoly Balogh (Charlie/SGR):
> There's no exception. At least not specifically for Result. It is simply
> variable passed by reference from the caller side, therefore it's not
> initialized inside the function.
You mean when I have the following:
--------------------------
function X : ansistring;
begin
end;
var S : ansistring;
S := X;
--------------------------
Then S is a dangling pointer to nowhere?
> And this is A., consistent with other var
> parameters B., apparently also how Delphi does it. Actually, since managed
> types are always passed by reference, this is not really a surprise, nor
> an exception.
Of course it is a surprise because the documenation says that managed types are always initialized.
If I use a var parameter I am forced to use a variable (declared somewhere else) but
the function result can also be used in expressions without any (direct) assignment to a variable.
So it must behave like a locally declared variable IMO.
If I use an intermediate variable like:
--------------------------
function X : ansistring;
var X1 : ansistring;
begin
Result := X1+'A';
end;
--------------------------
then it is initialized while here
--------------------------
function X : ansistring;
begin
Result := Result+'A';
end;
--------------------------
I get garbage?
That's a very strange behaviour and no longer Pascal, it's C with all its funny side effects.
> var
> a: ansistring;
> a:=some_function(a);
When you have a parameter then of course it depends
on how the parameter is declared.
The Result variable within a function is different
and not a parameter. You cannot feed in any data here.
I would consider this more like an out parameter
but in case of managed types an initialization
must take place somewhere (IMO in the function like
for all other local variables too, where else?).
And it is definitly a change with FPC 3 because all my programs relied
on that managed types are initialized and suddenly they are not anymore.
I am wondering what other surprises lurk here and there which I have just not found out.
More information about the fpc-pascal
mailing list