[fpc-devel] Maybe room for better documentation? open array as var param

Michael Van Canneyt michael at freepascal.org
Thu Jul 20 19:24:00 CEST 2023



On Thu, 20 Jul 2023, Martin Frb via fpc-devel wrote:

> For const param, it is well documented that the value (that includes the 
> variable that is passed) must not be changed.
>
> But for "var param"?
>
> Well maybe, but not explicit
> https://www.freepascal.org/docs-html/ref/refsu68.html#x184-20800014.4.5
> >> Open parameters can be passed by value, by reference or as a 
> constant parameter. In the latter cases the procedure receives a pointer 
> to the actual array.
>
> So a user with sufficient experience could detect that if a pointer is 
> received, then the value which is pointed to must not be changed.
>
> Maybe that should be mentioned more explicitly.
> And maybe it should additionally also be mentioned on 
> https://www.freepascal.org/docs-html/ref/refsu65.html
>
>
> Because the below may be unexpected to quite a few users.
>
> It will (at least on my test on windows / of course depends on mem 
> manager) print numbers starting at 300.
> Even so 200++ has been assigned.
>
> But (with sufficient luck or lack of luck) "y" will re-use the memory of 
> "x". And "a" will then change "y" which may not be expected.
>
>
> program Project1;
> {$mode objfpc}
>
> var x,y: array of integer;
>   i: Integer;
>
> procedure foo(var a: array of integer);
> var
>   j: Integer;
> begin
>   x := nil;
>   SetLength(y, 10);
>   for j := 0 to 9 do y[j] := 200+j;
>
>   for j := 0 to 9 do a[j] := 300+j;
> end;
>
> begin
>   SetLength(x, 10);
>   for i := 0 to 9 do x[i] := 100+i;
>   foo(x);
>   for i := 0 to 9 do writeln(y[i]);
>   readln;
> end.

It's IMO probably better to outright forbid passing open array by reference.

printing length(a) after x:=Nil; gives 10, which is simply wrong.

Michael.


More information about the fpc-devel mailing list