[SOLVED] Dynamic Array as Parameter lazarus / 21-General ---------------------------------------------------------------------- Handoko · 2022-06-15 04:51 Recently, there was a case supplying a fixed-length array to a function that requires a dynamic array. No compile-time error and no hint but the code didn't run as what we expected. This is the post: https://forum.lazarus.freepascal.org/index.php/topic,59628.msg444645.h tml#msg444645 I checked the documentation: https://wiki.freepascal.org/Dynamic_array It doesn't say that is allowed or not. So the question is: Is it an unimplemented feature or bug, why we can supply a fixed- length array to a function that require a dynamic array? ---------------------------------------------------------------------- bytebites · 2022-06-15 05:48 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i QuickSort(var AI: array of Integer; ALo, AHi: Integer); This does not require dynamic array. It is open array: https://www.freepascal.org/docs-html/ref/refsu68.html ---------------------------------------------------------------------- WooBean · 2022-06-15 06:20 Hi Handoko, as your link to forum points to nowhere I have made some tests as below Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program dynArr;//Lazarus 2.2.0 (rev lazarus_2_2_0) FPC 3.2.2 x86_64-win64-win32/win64uses sysutils;var a:TBytes;  b:array [0..3] of byte =(1,2,3,4);  c:array [1..4] of byte =(1,2,3,4);procedure test1(dA:TBytes);begin  write('parameter by value: length=',length(dA));  setlength(dA,length(dA)+1);  writeln(', +1 -> =',length(dA));end;procedure test2( var dA:TBytes);begin  write('parameter by var: length=',length(dA));  setlength(dA,length(dA)+1);  writeln(', +1 -> =',length(dA));end; begin  a:=nil;  test1(a);  test1(a);  test2(a);  test2(a);  test1(b); //can use static array (zero based index) as a parameter - what is OK,  //and can change(extend) length of static array - what is inaccceptable  //test2(b);//Compilation Error: Call by var for arg no. 1 has to match exactly: Got "Array[0..3] Of Byte" expected "TBytes"  //test1(c);//Compilation Error: Incompatible type for arg no. 1: Got "Array[1..4] Of Byte", expected "TBytes"   readln;end.  Program output: parameter by value: length=0, +1 -> =1 parameter by value: length=0, +1 -> =1 parameter by var: length=0, +1 -> =1 parameter by var: length=1, +1 -> =2 parameter by value: length=4, +1 -> =5 I think we have a bug pinpointed. ---------------------------------------------------------------------- Handoko · 2022-06-15 06:55 Problem solved. Actually there's no problem but I need to refresh my memory. As mention by bytebites, there is something called open array. I forgot how it works. No bug, no problem, only my own confusion.  Thank you all. ---------------------------------------------------------------------- PascalDragon · 2022-06-15 07:00 Quote from: WooBean on June 15, 2022, 08:20:54 amI think we have a bug pinpointed. It's not a bug, it's simply the behaviour of the implementation details of how the static array to dynamic array conversion works, namely by creating a temporary, dynamic array. This is also why this functionality was disabled by default in 3.3.1 and can be enabled using $ModeSwitch ArrayToDynArray. Please note however that Handoko was talking about open array parameters. @Handoko: when you pass a static array to a function taking an open array the called function always receives a 0-based array with the same length, cause in essence an open array is a pointer to the first element and the length of the array (or more precisely the High value). Thus for a function taking an open array it's completely transparent what is passed to it: a static array, a dynamic array, another open array (including slices) or even a single element. You need to take care however when you pass indices as additional parameters like is done in the example you mentioned, cause those will be wrong then. ---------------------------------------------------------------------- Handoko · 2022-06-15 07:11 Thank you for the explanation. It had been long time I did not use open array, I now mainly use container classes, like TList, etc. So I forgot how open array works. And yes, for the case WooBean mentioned - it is good to be disabled by default on future releases.