[fpc-pascal] Array as result in function.

Jürgen Hestermann juergen.hestermann at gmx.de
Fri Jan 20 12:56:46 CET 2017


Am 2017-01-20 um 07:52 schrieb Martin Schreiber:
 > On Thursday 19 January 2017 22:50:36 fredvs wrote:
 >> function array_in_out(arrayin: TArFloat): TArFloat;
 >> begin
 >> result := arrayin;
 >> end;
 > Do you change items of "arrayin" later? If so the items of the result array
 > will be changed too, dynamic array assignment copies the data pointer only.
 > http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1
 > Use
 > "
 > function array_in_out(arrayin: TArFloat): TArFloat;
 > begin
 >  result:= copy(arrayin);
 > end;
 > "
 > if the result array must be independent.

While the original Pascal language was clear and logical,
it has become ambiguous with managed types.

In this declaration

var X : int64;

"X" always means the 8 bytes that hold the integer.
"@X" always means the 4 bytes of the *address* where the 8 bytes start.
Here you are always aware what is meant:
Either the data or the address of the data (pointer).

In this declaration:

var A : array of int64;

"A" means the (elements of the) array if you index it as in "A[7]"
but it means the (4 byte) pointer to the first element if you use it
as function parameter or in assignments.

And even worse, *some* managed types (like strings) have "copy-on-write".

IMO this is all very confusing and leads to a lot of hard to spot bugs
but it cannot be changed anymore.




More information about the fpc-pascal mailing list