Stange behaviour of open arrays
Original Reporter info from Mantis: SimBen
-
Reporter name:
Original Reporter info from Mantis: SimBen
- Reporter name:
Description:
When using a user-defined type, open arrays are passed by reference even if they are asked to be passed by values. This is probably unimportant in most cases, but can be potentially dangerous when the user thinks the array has not been modified by the code whereas in reallity it may have been.
In the example below, the procedure "bibi", which does not use a user-defined type, behaves correctly, whereas the procedure "bobo", which is based on the user-defined type T=array of byte, behaves as if the open array was passed by reference instead of by value, i.e. as if this procedure was written:
procedure bobo(var A: T);
begin inc(s[1]); end;
Steps to reproduce:
type T=array of byte;
var x: T;
procedure bibi(A: array of byte);
begin inc(A[1]); end;
procedure bobo(A: T);
begin inc(A[1]); end;
begin
setlength(x,2); x[1]:=6;
bibi(x); writeln(x[1]); //result=6 OK (array passed by value)
bobo(x); writeln(x[1]); //result=7; should be equal to 6 !!!
end.
Mantis conversion info:
- Mantis ID: 19204
- OS: Windows
- OS Build: XP SP3
- Platform: PC
- Version: 2.4.2